Process LaTeX code only if package is loaded
I would like to create a document used to test diverse packages, and I was wondering if it was possible to use a command so that part of the code is read only if the corresponding package is loaded. I've come up with this but it throws an error which I don't know how to fix.
documentclass{article}
usepackage[utf8]{inputenc}
makeatletter
newcommand{IfPackageLoaded}[3]{@ifpackageloaded{#1}{#2}{#3}}
makeatother
begin{document}
IfPackageLoaded{natbib}{natbib loaded}{natbib not loaded}
end{document}
I get this error:
! LaTeX Error: Can be used only in preamble.
See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
l.10 IfPackageLoaded{natbib}{natbib loaded}
                                            {natbib not loaded}
Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.
I also know that the command ltx@ifpackageloaded exists when loading the package ltxcmds but when I try to use it in
documentclass{article}
usepackage[utf8]{inputenc}
usepackage{ltxcmds}
begin{document}
ltx@ifpackageloaded{natbib}{natbib loaded}{natbib not loaded}
end{document}
I get this error message:
! Undefined control sequence.
l.11 ltx
         @ifpackageloaded{natbib}{natbib loaded}{natbib not loaded}
The control sequence at the end of the top line
of your error message was never def'ed. If you have
misspelled it (e.g., `hobx'), type `I' and the correct
spelling (e.g., `Ihbox'). Otherwise just continue,
and I'll forget about whatever was undefined.
Can someone please point out what I did wrong?
EDIT: changed documentclass from minimal to article.
packages
New contributor
lashoun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I would like to create a document used to test diverse packages, and I was wondering if it was possible to use a command so that part of the code is read only if the corresponding package is loaded. I've come up with this but it throws an error which I don't know how to fix.
documentclass{article}
usepackage[utf8]{inputenc}
makeatletter
newcommand{IfPackageLoaded}[3]{@ifpackageloaded{#1}{#2}{#3}}
makeatother
begin{document}
IfPackageLoaded{natbib}{natbib loaded}{natbib not loaded}
end{document}
I get this error:
! LaTeX Error: Can be used only in preamble.
See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
l.10 IfPackageLoaded{natbib}{natbib loaded}
                                            {natbib not loaded}
Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.
I also know that the command ltx@ifpackageloaded exists when loading the package ltxcmds but when I try to use it in
documentclass{article}
usepackage[utf8]{inputenc}
usepackage{ltxcmds}
begin{document}
ltx@ifpackageloaded{natbib}{natbib loaded}{natbib not loaded}
end{document}
I get this error message:
! Undefined control sequence.
l.11 ltx
         @ifpackageloaded{natbib}{natbib loaded}{natbib not loaded}
The control sequence at the end of the top line
of your error message was never def'ed. If you have
misspelled it (e.g., `hobx'), type `I' and the correct
spelling (e.g., `Ihbox'). Otherwise just continue,
and I'll forget about whatever was undefined.
Can someone please point out what I did wrong?
EDIT: changed documentclass from minimal to article.
packages
New contributor
lashoun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
You are missing themakeatletterin your second example. And don't use the minimal class, normally it is too minimal.
– Ulrike Fischer
2 days ago
Read more: tex.stackexchange.com/q/8351/156344
– JouleV
2 days ago
It is said in theltxcmdsdocumentation thatltx@ifpackageloadedcan be used afterbegin{document}though.
– lashoun
2 days ago
But even then it can only be used if you wrap it inmakeatletter...makeatotherbecause it has an@in its name.
– moewe
2 days ago
Oooh, that's why! I'll try then.
– lashoun
2 days ago
add a comment |
I would like to create a document used to test diverse packages, and I was wondering if it was possible to use a command so that part of the code is read only if the corresponding package is loaded. I've come up with this but it throws an error which I don't know how to fix.
documentclass{article}
usepackage[utf8]{inputenc}
makeatletter
newcommand{IfPackageLoaded}[3]{@ifpackageloaded{#1}{#2}{#3}}
makeatother
begin{document}
IfPackageLoaded{natbib}{natbib loaded}{natbib not loaded}
end{document}
I get this error:
! LaTeX Error: Can be used only in preamble.
See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
l.10 IfPackageLoaded{natbib}{natbib loaded}
                                            {natbib not loaded}
Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.
I also know that the command ltx@ifpackageloaded exists when loading the package ltxcmds but when I try to use it in
documentclass{article}
usepackage[utf8]{inputenc}
usepackage{ltxcmds}
begin{document}
ltx@ifpackageloaded{natbib}{natbib loaded}{natbib not loaded}
end{document}
I get this error message:
! Undefined control sequence.
l.11 ltx
         @ifpackageloaded{natbib}{natbib loaded}{natbib not loaded}
The control sequence at the end of the top line
of your error message was never def'ed. If you have
misspelled it (e.g., `hobx'), type `I' and the correct
spelling (e.g., `Ihbox'). Otherwise just continue,
and I'll forget about whatever was undefined.
Can someone please point out what I did wrong?
EDIT: changed documentclass from minimal to article.
packages
New contributor
lashoun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I would like to create a document used to test diverse packages, and I was wondering if it was possible to use a command so that part of the code is read only if the corresponding package is loaded. I've come up with this but it throws an error which I don't know how to fix.
documentclass{article}
usepackage[utf8]{inputenc}
makeatletter
newcommand{IfPackageLoaded}[3]{@ifpackageloaded{#1}{#2}{#3}}
makeatother
begin{document}
IfPackageLoaded{natbib}{natbib loaded}{natbib not loaded}
end{document}
I get this error:
! LaTeX Error: Can be used only in preamble.
See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
l.10 IfPackageLoaded{natbib}{natbib loaded}
                                            {natbib not loaded}
Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.
I also know that the command ltx@ifpackageloaded exists when loading the package ltxcmds but when I try to use it in
documentclass{article}
usepackage[utf8]{inputenc}
usepackage{ltxcmds}
begin{document}
ltx@ifpackageloaded{natbib}{natbib loaded}{natbib not loaded}
end{document}
I get this error message:
! Undefined control sequence.
l.11 ltx
         @ifpackageloaded{natbib}{natbib loaded}{natbib not loaded}
The control sequence at the end of the top line
of your error message was never def'ed. If you have
misspelled it (e.g., `hobx'), type `I' and the correct
spelling (e.g., `Ihbox'). Otherwise just continue,
and I'll forget about whatever was undefined.
Can someone please point out what I did wrong?
EDIT: changed documentclass from minimal to article.
packages
packages
New contributor
lashoun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
lashoun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 2 days ago
lashoun
New contributor
lashoun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 2 days ago
lashounlashoun
615
615
New contributor
lashoun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
lashoun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
lashoun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
You are missing themakeatletterin your second example. And don't use the minimal class, normally it is too minimal.
– Ulrike Fischer
2 days ago
Read more: tex.stackexchange.com/q/8351/156344
– JouleV
2 days ago
It is said in theltxcmdsdocumentation thatltx@ifpackageloadedcan be used afterbegin{document}though.
– lashoun
2 days ago
But even then it can only be used if you wrap it inmakeatletter...makeatotherbecause it has an@in its name.
– moewe
2 days ago
Oooh, that's why! I'll try then.
– lashoun
2 days ago
add a comment |
2
You are missing themakeatletterin your second example. And don't use the minimal class, normally it is too minimal.
– Ulrike Fischer
2 days ago
Read more: tex.stackexchange.com/q/8351/156344
– JouleV
2 days ago
It is said in theltxcmdsdocumentation thatltx@ifpackageloadedcan be used afterbegin{document}though.
– lashoun
2 days ago
But even then it can only be used if you wrap it inmakeatletter...makeatotherbecause it has an@in its name.
– moewe
2 days ago
Oooh, that's why! I'll try then.
– lashoun
2 days ago
2
2
You are missing the
makeatletter in your second example. And don't use the minimal class, normally it is too minimal.– Ulrike Fischer
2 days ago
You are missing the
makeatletter in your second example. And don't use the minimal class, normally it is too minimal.– Ulrike Fischer
2 days ago
Read more: tex.stackexchange.com/q/8351/156344
– JouleV
2 days ago
Read more: tex.stackexchange.com/q/8351/156344
– JouleV
2 days ago
It is said in the
ltxcmds documentation that ltx@ifpackageloaded can be used after begin{document} though.– lashoun
2 days ago
It is said in the
ltxcmds documentation that ltx@ifpackageloaded can be used after begin{document} though.– lashoun
2 days ago
But even then it can only be used if you wrap it in
makeatletter...makeatother because it has an @ in its name.– moewe
2 days ago
But even then it can only be used if you wrap it in
makeatletter...makeatother because it has an @ in its name.– moewe
2 days ago
Oooh, that's why! I'll try then.
– lashoun
2 days ago
Oooh, that's why! I'll try then.
– lashoun
2 days ago
add a comment |
                                1 Answer
                            1
                        
active
oldest
votes
As users Ulrike Fischer and moewe pointed out, this works:
documentclass{article}
usepackage[utf8]{inputenc}
usepackage{ltxcmds}
makeatletter
newcommand{IfPackageLoaded}[3]{ltx@ifpackageloaded{#1}{#2}{#3}}
makeatother
begin{document}
IfPackageLoaded{natbib}{natbib loaded}{natbib not loaded}
end{document}
New contributor
lashoun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
BTW: The[3]is not needed. You can simply usenewcommand*{IfPackageLoaded}{ltx@ifpackageloaded}.
– Schweinebacke
2 days ago
@Schweinebacke RIght; alsoletIfPackageLoadedltx@ifpackageloaded
– egreg
2 days ago
@egreg Yes, becauseltx@ifpackageloadedis a simple macro. If it would be, e.g., robust, it could be better to useLetLtxMacrofrom packageletltxmacro. I didn't want to explain all this, so I've only noted about usingnewcommand*without arguments.
– Schweinebacke
2 days ago
Thanks for the precisions
– lashoun
2 days ago
add a comment |
                                Your Answer
                            
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
lashoun is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f484088%2fprocess-latex-code-only-if-package-is-loaded%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
                                1 Answer
                            1
                        
active
oldest
votes
                                1 Answer
                            1
                        
active
oldest
votes
active
oldest
votes
active
oldest
votes
As users Ulrike Fischer and moewe pointed out, this works:
documentclass{article}
usepackage[utf8]{inputenc}
usepackage{ltxcmds}
makeatletter
newcommand{IfPackageLoaded}[3]{ltx@ifpackageloaded{#1}{#2}{#3}}
makeatother
begin{document}
IfPackageLoaded{natbib}{natbib loaded}{natbib not loaded}
end{document}
New contributor
lashoun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
BTW: The[3]is not needed. You can simply usenewcommand*{IfPackageLoaded}{ltx@ifpackageloaded}.
– Schweinebacke
2 days ago
@Schweinebacke RIght; alsoletIfPackageLoadedltx@ifpackageloaded
– egreg
2 days ago
@egreg Yes, becauseltx@ifpackageloadedis a simple macro. If it would be, e.g., robust, it could be better to useLetLtxMacrofrom packageletltxmacro. I didn't want to explain all this, so I've only noted about usingnewcommand*without arguments.
– Schweinebacke
2 days ago
Thanks for the precisions
– lashoun
2 days ago
add a comment |
As users Ulrike Fischer and moewe pointed out, this works:
documentclass{article}
usepackage[utf8]{inputenc}
usepackage{ltxcmds}
makeatletter
newcommand{IfPackageLoaded}[3]{ltx@ifpackageloaded{#1}{#2}{#3}}
makeatother
begin{document}
IfPackageLoaded{natbib}{natbib loaded}{natbib not loaded}
end{document}
New contributor
lashoun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
BTW: The[3]is not needed. You can simply usenewcommand*{IfPackageLoaded}{ltx@ifpackageloaded}.
– Schweinebacke
2 days ago
@Schweinebacke RIght; alsoletIfPackageLoadedltx@ifpackageloaded
– egreg
2 days ago
@egreg Yes, becauseltx@ifpackageloadedis a simple macro. If it would be, e.g., robust, it could be better to useLetLtxMacrofrom packageletltxmacro. I didn't want to explain all this, so I've only noted about usingnewcommand*without arguments.
– Schweinebacke
2 days ago
Thanks for the precisions
– lashoun
2 days ago
add a comment |
As users Ulrike Fischer and moewe pointed out, this works:
documentclass{article}
usepackage[utf8]{inputenc}
usepackage{ltxcmds}
makeatletter
newcommand{IfPackageLoaded}[3]{ltx@ifpackageloaded{#1}{#2}{#3}}
makeatother
begin{document}
IfPackageLoaded{natbib}{natbib loaded}{natbib not loaded}
end{document}
New contributor
lashoun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
As users Ulrike Fischer and moewe pointed out, this works:
documentclass{article}
usepackage[utf8]{inputenc}
usepackage{ltxcmds}
makeatletter
newcommand{IfPackageLoaded}[3]{ltx@ifpackageloaded{#1}{#2}{#3}}
makeatother
begin{document}
IfPackageLoaded{natbib}{natbib loaded}{natbib not loaded}
end{document}
New contributor
lashoun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 2 days ago
Schweinebacke
22.6k4577
22.6k4577
New contributor
lashoun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 2 days ago
lashounlashoun
615
615
New contributor
lashoun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
lashoun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
lashoun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
BTW: The[3]is not needed. You can simply usenewcommand*{IfPackageLoaded}{ltx@ifpackageloaded}.
– Schweinebacke
2 days ago
@Schweinebacke RIght; alsoletIfPackageLoadedltx@ifpackageloaded
– egreg
2 days ago
@egreg Yes, becauseltx@ifpackageloadedis a simple macro. If it would be, e.g., robust, it could be better to useLetLtxMacrofrom packageletltxmacro. I didn't want to explain all this, so I've only noted about usingnewcommand*without arguments.
– Schweinebacke
2 days ago
Thanks for the precisions
– lashoun
2 days ago
add a comment |
2
BTW: The[3]is not needed. You can simply usenewcommand*{IfPackageLoaded}{ltx@ifpackageloaded}.
– Schweinebacke
2 days ago
@Schweinebacke RIght; alsoletIfPackageLoadedltx@ifpackageloaded
– egreg
2 days ago
@egreg Yes, becauseltx@ifpackageloadedis a simple macro. If it would be, e.g., robust, it could be better to useLetLtxMacrofrom packageletltxmacro. I didn't want to explain all this, so I've only noted about usingnewcommand*without arguments.
– Schweinebacke
2 days ago
Thanks for the precisions
– lashoun
2 days ago
2
2
BTW: The
[3] is not needed. You can simply use newcommand*{IfPackageLoaded}{ltx@ifpackageloaded}.– Schweinebacke
2 days ago
BTW: The
[3] is not needed. You can simply use newcommand*{IfPackageLoaded}{ltx@ifpackageloaded}.– Schweinebacke
2 days ago
@Schweinebacke RIght; also
letIfPackageLoadedltx@ifpackageloaded– egreg
2 days ago
@Schweinebacke RIght; also
letIfPackageLoadedltx@ifpackageloaded– egreg
2 days ago
@egreg Yes, because
ltx@ifpackageloaded is a simple macro. If it would be, e.g., robust, it could be better to use LetLtxMacro from package letltxmacro. I didn't want to explain all this, so I've only noted about using newcommand* without arguments.– Schweinebacke
2 days ago
@egreg Yes, because
ltx@ifpackageloaded is a simple macro. If it would be, e.g., robust, it could be better to use LetLtxMacro from package letltxmacro. I didn't want to explain all this, so I've only noted about using newcommand* without arguments.– Schweinebacke
2 days ago
Thanks for the precisions
– lashoun
2 days ago
Thanks for the precisions
– lashoun
2 days ago
add a comment |
lashoun is a new contributor. Be nice, and check out our Code of Conduct.
lashoun is a new contributor. Be nice, and check out our Code of Conduct.
lashoun is a new contributor. Be nice, and check out our Code of Conduct.
lashoun is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
 
But avoid …
- Asking for help, clarification, or responding to other answers.
 - Making statements based on opinion; back them up with references or personal experience.
 
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f484088%2fprocess-latex-code-only-if-package-is-loaded%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You are missing the
makeatletterin your second example. And don't use the minimal class, normally it is too minimal.– Ulrike Fischer
2 days ago
Read more: tex.stackexchange.com/q/8351/156344
– JouleV
2 days ago
It is said in the
ltxcmdsdocumentation thatltx@ifpackageloadedcan be used afterbegin{document}though.– lashoun
2 days ago
But even then it can only be used if you wrap it in
makeatletter...makeatotherbecause it has an@in its name.– moewe
2 days ago
Oooh, that's why! I'll try then.
– lashoun
2 days ago