2

I try to autodefine the preamble using this and that approach but do get Undefined control sequence. \@ifundefined{\KOMAClassName } in case I write report for scrreprt. for both options.

\documentclass[12pt,a4paper,notitlepage,oneside]{report}
%NOTE Packages, my Macros und Formatdefinitions
    \usepackage[T1]{fontenc}            % Allows different font encodings and hyphenation -> ctan.org/pkg/fontenc
    \usepackage[utf8]{inputenc}         % Translates input encodings into LaTeX internal language -> ctan.org/pkg/inputenc
    \usepackage{blindtext}
    \makeatletter
        \@ifundefined{KOMAClassName }{\newcommand{\test}{num1} }{\newcommand{\test}{num2} } %option1
        \@ifclassloaded{KOMAClassName }{\newcommand{\testb}{num1} }{\newcommand{\testb}{num2} } %option2
    \makeatother
\begin{document}
\section{hi}
\test \\
\testb\\
\end{document}

Using scrreprt or report I get printed num1 and num2 for \test and \testb (which isnt also the case)

  • \@ifundefined{KOMAClassName}{...}{...}, i.e. no backslash – user31729 Apr 11 '16 at 19:55
  • Well, you need \@ifclassloaded{\KOMAClassName}{...}{...}, since this does not expect a macro name. Your code tests whether the class KOMAClassName is loaded, which is not the case – user31729 Apr 11 '16 at 20:00
3

Use \@ifundefined{KOMAClassName}{...}{...} since \@ifundefined calls

\expandafter\ifx\csname #1 \endcsname (after a test), i.e. it constructs the command sequence.

Here's the definition in latex.ltx.

\def\@ifundefined#1{%
  \expandafter\ifx\csname#1\endcsname\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

But for \@ifclassloaded you need the class name, i.e. what \KOMAClassName provides for you.

The difference between the commands is just that \@ifundefined checks for a command name comparing it with \relax whereas \@ifclassloaded checks for a token, i.e. a class name. KOMAClassName means a class named KOMAClassName and not what \KOMAClassName provides (scrreprt, expectedly?)

I am not sure what the \test and \testb commands should do, but I think the setup should be something like

\documentclass[12pt,a4paper,notitlepage,oneside]{scrreprt}
%NOTE Packages, my Macros und Formatdefinitions
    \usepackage[T1]{fontenc}            % Allows different font encodings and hyphenation -> ctan.org/pkg/fontenc
    \usepackage[utf8]{inputenc}         % Translates input encodings into LaTeX internal language -> ctan.org/pkg/inputenc
    \usepackage{blindtext}
%    \providecommand{\test}{}
%    \providecommand{\testb{}
    \newif\ifstandardreport
    \makeatletter
    \@ifundefined{KOMAClassName}{%
      \standardreporttrue
      \newcommand{\test}{num1} 
    }{%
      \newcommand{\test}{num2}  %option1
    }
    \ifstandardreport
    \newcommand{\testb}{num1} 
    \else
    \@ifclassloaded{\KOMAClassName}{%
      \newcommand{\testb}{num2}
    }{
      \newcommand{\testb}{num1} 
    } %option2
    \fi
    \makeatother
\begin{document}
\section{hi}
\test \\
\testb\\
\end{document}

Alternatively use \providecommand{\test}{...} and do \renewcommand{\test}{...} later on in the \@ifclassloaded tests.

  • Of course, the \KOMAClassName test must fail if the class is not a KOMA class – user31729 Apr 11 '16 at 20:10
  • So \@ifclassloaded{\KOMAClassName} without interweaving it with other solutions (\@ifundefined and \newif) can not be used for that purpose as \KOMAClassName would be undefined -> compile error – qrtLs Apr 11 '16 at 20:41
  • 1
    @ItsmeJulian: Of course not, see my possible solution I've posted already half an hour ago, which circumvents this. Blame KOMA, not LaTeX ;-) – user31729 Apr 11 '16 at 20:44
  • @ItsmeJulian KOMAClassName is not the name of any class, so it is useless and pointless to test for it. This is neither LaTeX nor KOMAs fault, this is simple user error. – Johannes_B Aug 6 '16 at 11:19
1

\KOMAClassName can be used to test if a KOMA-class is used, as they all define that command. It cannot be used to test for a special class.

\documentclass[12pt,a4paper,notitlepage,oneside]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{blindtext}
\makeatletter
\@ifundefined{KOMAClassName}{%
    \typeout{A standard class is used, but which one?}
    \@ifclassloaded{article}{%
        \typeout{This is an article document}
    }{%
        \@ifclassloaded{report}{%
            \typeout{This is a report document}
        }{%
            \@ifclassloaded{book}{%
                \typeout{This is a book document}
            }{%
                \@ifclassloaded{memoir}{%
                    \typeout{Memoir is in use}
                }{%
                    \typeout{Something else is going on}
                }
            }
        }
    }
}{%
        \typeout{A KOMA class is used, but which one?}
        \@ifclassloaded{scrartcl}{\typeout{scrartcl is used}
    }{%
        \@ifclassloaded{scrreprt}{%
            \typeout{scrreprt is used}
        }{%
            \@ifclassloaded{scrbook}{scrbook is loaded}{\typeout{Something else is going on}
            \@ifclassloaded{memoir}{\typeout{Memoir? In this branch? Impossible}
        }{}
    }
}
}
}
\@ifclassloaded{KOMAClassName}{\typeout{Wait? What?}}{\typeout{There is no class with that name}}
\makeatother
\begin{document}
Wombat
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.