# Which document class is being used?

Is there a way to programmatically determine which document class is in effect?

I'd like to be able to write conditional macros that behave differently depending on which document class is currently being used.

-

You want the LaTeX kernel function \@ifclassloaded:

\@ifclassloaded{<someclass>}
{<true code>}
{<false coode>}%

-
I am sure this is obvious for most people who have reason to use this but it was not for me (who wanted to hack a package out of the preamble when beamer is used): for this to work, you have to enclose it in \makeatletter and \makeatother. –  Raphael Feb 23 '12 at 10:27

If you want to differentiate between KOMA-Script-classes (scrartcl, scrbook, scrreprt, scrlttr2) and other classes, you may also test for \KOMAClassName (which is defined by the former classes):

\@ifundefined{KOMAClassName}
{<code for other classes>}
{<code for KOMA-Script-classes>}

-

If your package executes \@getclass then the current class name will be stored in \@currentclass which you can then test or print out as required (this is probably more general than using \@ifclassloaded which requires a class name in advance). You can define \@getclass like so

\def\@getcl@ss#1.cls#2\relax{\def\@currentclass{#1}}
\def\@getclass{\expandafter\@getcl@ss\@filelist\relax}


and use like:

\@getclass
\typeout{this document uses \@currentclass\space class}

-

Actually a class is loaded with the same internal macros like packages (both are officially "files with options") and LaTeX doesn't store the class file name in a specific macro. So you can't directly test which class was used, only test whether a specific class was used or not using \@ifclassloaded{<class>}{<true>}{<false>} (already mentioned in a previous answer).

Also keep in mind that a class can be based on another class by loading it with \LoadClass or \LoadClassWithOptions. In this case \@ifclassloaded will be true for both. However AFAIK the standard classes are not based on any other classes.

-
Well, the standard ltxdoc document class is based on article. :-) –  Andrey Vihrov May 9 '11 at 20:20

I came up with the following scenario, which isn't advisable in practice but which is of theoretical interest. David Carlisle's solution still works in this case but doesn't tell the complete story.

% Class file:
\ProvidesClass{testclass}[2012/02/05 v0.1 A test article class (AM)]
\NeedsTeXFormat{LaTeX2e}[2011/06/27]
\RequirePackage{catoptions}
\XDeclareOption*{%
\ClassWarning{testclass}{Unknown option '\CurrentOption' ignored}%
}
\XProcessOptions*\relax
\endinput

% Document file:
\documentclass{testclass}
\begin{document}
x
\end{document}


David Carlisle's solution

\def\@getcl@ss#1.cls#2\relax{\def\@currentclass{#1}}
\def\@getclass{\expandafter\@getcl@ss\@filelist\relax}
\@getclass
\typeout{This document uses class: \@currentclass}


gives 'article' as the class, while in reality it may be 'testclass' that the user needs. So my approach is to find all the classes in effect, upon which further action can be taken:

\robust@def*\@getdocumentclass{%
\undefcs\currentclass
\docommalist*\@filelist{%
\filename@parse{##1}%
\ifxFT\filename@ext\@clsextension{}{%
\ifcsndefFT{opt@\filename@area\filename@base.\filename@ext}{}{%
\edef\currentclass{%
\csliststack,\currentclass
\filename@area\filename@base.\filename@ext
}%
}%
}%
}%
\ifdefTF\currentclass{%
\typeout{This document uses class(es) in this order: \currentclass}%
}{%
\string\documentclass}%
}%
}
\robust@def*\getdocumentclass{%
\ifxTF\@filelist\relax{}{%
\ifxTF\@filelist\@gobble{}{%
\ifcsemptyTF\@filelist{}\@getdocumentclass
}%
}%
}


This can be called simply as

\getdocumentclass

-

Expanding on Ahmed's idea, here's some code with LaTeX3:

\RequirePackage{expl3}

\ExplSyntaxOn
\seq_new:N \l_pclist_classes_seq
\clist_map_inline:cn { @filelist }
{
\tl_if_in:nnT { #1 } { .cls }
{
\tl_set:Nn \l_tmpa_tl { #1 }
\tl_remove_once:Nn \l_tmpa_tl { .cls }
\seq_put_right:NV \l_pclist_classes_seq \l_tmpa_tl
}
}

\seq_if_in:NnTF \l_pclist_classes_seq { article }
{ \msg_term:x { The~class~article'~is~loaded } }
{ \msg_term:x { The~class~article'~is~NOT~loaded } }

\ExplSyntaxOff


The final lines show how the information can be used, by testing whether a class name appears in the sequence. One can similarly test what packages have been loaded so far.

Here's a more complete code, where three sequences are built where classes, packages and other files are stored, respectively:

\documentclass{ltxdoc}
\RequirePackage{expl3}

\ExplSyntaxOn
\seq_new:N \l_pclist_classes_seq
\seq_new:N \l_pclist_packages_seq
\seq_new:N \l_pclist_other_seq
\clist_map_inline:cn { @filelist }
{
\tl_if_in:nnTF { #1 } { .cls }
{
\tl_set:Nn \l_tmpa_tl { #1 }
\tl_remove_once:Nn \l_tmpa_tl { .cls }
\seq_put_right:NV \l_pclist_classes_seq \l_tmpa_tl
}
{
\tl_if_in:nnTF { #1 } { .sty }
{
\tl_set:Nn \l_tmpa_tl { #1 }
\tl_remove_once:Nn \l_tmpa_tl { .sty }
\seq_put_right:NV \l_pclist_packages_seq \l_tmpa_tl
}
{
\seq_put_right:Nn \l_pclist_other_seq { #1 }
}
}
}

\seq_if_in:NnTF \l_pclist_classes_seq { article }
{ \msg_term:x { The~class~article'~is~loaded } }
{ \msg_term:x { The~class~article'~is~NOT~loaded } }

\seq_show:N \l_pclist_classes_seq
\seq_show:N \l_pclist_packages_seq
\seq_show:N \l_pclist_other_seq
\ExplSyntaxOff


The example uses ltxdoc as base class in order to show that it's item 0 in the "classes" sequence.

-