[Tex/LaTex] Check what kind of document class is used

conditionalsdocument-classesletterstitling

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}

Best Answer

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}
Related Question