我可以知道文档(或用户定义的包)中的类选项吗?

我可以知道文档(或用户定义的包)中的类选项吗?

再次编辑。看来我不太清楚。我正在寻找一种技术来访问传递给类的选项。下面的 MWE 只是一个例子,12pt 就是一个例子。如果没有通用技术,那么我想要检查的特定选项是 11pt、12pt 和 twoside。

我能否在文档中确定 12pt 选项(或其他选项,如 twoside)是否已传递给类?像这样:

\documentclass[12pt]{article}
\makeatletter
\newif\if@myxiipt\@myxiiptfalse

% [how to set if@myxiipt ???]

\if@myxiipt
\usepackage{ebgaramond}% Lovely font, but so small I only use if 12pt
\else
\usepackage{XCharter}% Font is legible at small sizes
\fi
\makeatother

\begin{document}
It was a dark and stormy night...
\end{document}

答案1

像这样吗?

\documentclass[12pt,twoside,11pt]{article}
\makeatletter
\newif\if@myxiipt\@myxiiptfalse
\def\tempa{12pt}\def\tempb{11pt}\def\tempc{twoside}%
\def\mysides{one}\def\mysize{normal}
\@for\xx:=\@classoptionslist\do{%
  \ifx\tempa\xx \def\mysize{biggest}\@myxiipttrue\fi
  \ifx\tempb\xx \def\mysize{bigger}\fi
  \ifx\tempc\xx \def\mysides{two}\fi
}

\if@myxiipt
  \usepackage{ebgaramond}
\else
  \usepackage{XCharter}
\fi
\makeatother

\begin{document}
The size chosen was \mysize.
The document is \mysides-sided.
It was a dark and stormy night \dots.
\end{document}

课程选项测试

答案2

最后编辑

\documentclass[twoside,12pt]{article}    

\makeatletter
\ifnum\f@size=12
\if@twoside
\usepackage{tikz}
\fi
\fi
\makeatother

\begin{document}


\begin{tikzpicture}
   \node at (0,0) {Package loaded for 12 pt and twosided document. In other cases the compilation will fail!};
\end{tikzpicture}


\end{document}

在 OP 编辑​​后,由于 documentclass 只接受字体大小的整数,因此有一个简单的方法

\documentclass[12pt]{article}    

\makeatletter
\ifnum\f@size=12
\usepackage{tikz}
\fi
\makeatother

\begin{document}


\begin{tikzpicture}
   \node at (0,0) {Package loaded for 12 pt!};
\end{tikzpicture}


\end{document}

维度是实数,因此您必须考虑检查小于和大于,因为并非所有十进制数都可以表示为二进制数:

您可以按照以下方式进行计算:

\documentclass[12pt]{article}
\usepackage{anyfontsize}
\makeatletter
\newlength{\mysize}
\newlength{\testSize}
\setlength{\testSize}{12pt}
\setlength{\mysize}{\f@size pt}
\makeatother


% [how to set if@myxiipt ???]
\makeatletter
\newcommand{\testTS}[1]{
\ifnum #1=0
\ifdim\mysize>\dimexpr0.999\testSize\relax
\ifdim\mysize<\dimexpr1.001\testSize\relax
Class loaded with 12pt option
\else
Class loaded without 12pt option
\fi
\else
Class loaded without 12pt option
\fi
\else
\xdef\mycursize{\f@size pt}
\ifdim\mycursize>\dimexpr0.999\testSize\relax
\ifdim\mycursize<\dimexpr1.001\testSize\relax
This is 12pt text.
\else
This is not 12pt text.
\fi
\else
This is not 12pt text.
\fi
\fi
}
\makeatother

\begin{document}
\testTS{0}
\testTS{1}

\fontsize{11.97}{14}\selectfont

\testTS{0}
\testTS{1}
\end{document}

相关内容