我尝试使用自动定义序言这和那方法,但确实得到,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}
使用scrreprt
或report
我打印出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}