检查 MWE 使用的是什么类型的文档类别

检查 MWE 使用的是什么类型的文档类别

我尝试使用自动定义序言方法,但确实得到,Undefined control sequence. \@ifundefined{\KOMAClassName }以防万一我为 scrreprt 写报告。针对这两个选项。

\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}

使用scrreprtreport我打印出num1和num2 \test\testb事实并非如此)

答案1

使用\@ifundefined{KOMAClassName}{...}{...}since\@ifundefined调用

\expandafter\ifx\csname #1 \endcsname(经过测试),即构建命令序列。

这是的定义latex.ltx

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

但是\@ifclassloaded您需要类名,即为\KOMAClassName您提供的名称。

命令之间的区别仅在于\@ifundefined检查命令名称并将其与检查标记(即类名)进行比较\relax\@ifclassloaded意味着KOMAClassName命名的类KOMAClassName而不是\KOMAClassName提供的内容(scrreprt,预期?)

我不确定\test\testb命令应该做什么,但我认为设置应该是这样的

\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}

或者在稍后的测试中使用\providecommand{\test}{...}并执行。\renewcommand{\test}{...}\@ifclassloaded

答案2

\KOMAClassName可用于测试是否使用了 KOMA 类,因为它们都定义了该命令。它不能用于测试特殊类。

\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}

相关内容