17

I have defined a .sty-file which I use in a report, which holds a lot of good information and styles for how my documents should look like. This have been working great, until when I tried making a letter. My own style loads the titling-package among others, and this seems to cause errors. I of course do not really need the titling-package in my letter, but it is necessary in my other documents. I was wondering if I could write some kind of check to only load the titling-package if the document class is not a letter. That way, it will work with all my other documents, and my letters.

In the included example, I have only loaded the titling-package, and not my personal style. The errors produced are the same.

\documentclass{letter}
\usepackage{lipsum, 
titling
}
\signature{Your name}
\address{Street \\ City \\ Country}
\begin{document}
\begin{letter}{Company name \\ Street\\ City\\ Country}
\opening{Dear Sir or Madam:}
\lipsum[1-2]
\closing{Yours Faithfully,}
\ps{P.S. Here goes your ps.}
\encl{Enclosures.}
\end{letter}
\end{document}
1
  • \@ifclassloaded{...} perhaps?
    – user31729
    Nov 19, 2015 at 20:26

1 Answer 1

20

The LaTeX core defines \@ifclassloaded{classname}{true branch}{false branch} which is basically the same as \@ifpackageloaded for checking whether a certain package is loaded.

The class name has to be specified without the usual .cls extension.

Since @ occurs in the macro name \@ifclassloaded, \makeatletter...\makeatother is needed in the document preamble, if the check occurs in a self-written package, \makeatletter...\makeatother must not appear there!

\documentclass{letter}
\usepackage{lipsum}

\makeatletter
\@ifclassloaded{letter}{%
  \typeout{this uses letter class}%
}{%
  \usepackage{titling}
  \typeout{this uses another class}%
}
\makeatother    



\signature{Your name}
\address{Street \\ City \\ Country}
\begin{document}
\begin{letter}{Company name \\ Street\\ City\\ Country}
\opening{Dear Sir or Madam:}
\lipsum[1-2]
\closing{Yours Faithfully,}
\ps{P.S. Here goes your ps.}
\encl{Enclosures.}
\end{letter}
\end{document}
2
  • 1
    @runartrollet: You're welcome. Happy TeXing!
    – user31729
    Nov 19, 2015 at 20:42
  • 1
    Sometime between 2015 and 2023, the public command \IfClassLoadedTF has been added to the kernel. It does the exact same thing as \@ifclassloaded but (a) doesn't require \makeatletter...\makeatother and (b) can be used outside of the preamble. Jun 21, 2023 at 19:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .