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., `I\hbox'). 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.
\makeatletterin your second example. And don't use the minimal class, normally it is too minimal. – Ulrike Fischer Apr 10 '19 at 09:52ltxcmdsdocumentation that\ltx@ifpackageloadedcan be used after\begin{document}though. – Lashoun Apr 10 '19 at 09:58\makeatletter...\makeatotherbecause it has an@in its name. – moewe Apr 10 '19 at 09:59