![平均能量损失](https://linux22.com/image/430345/%E5%B9%B3%E5%9D%87%E8%83%BD%E9%87%8F%E6%8D%9F%E5%A4%B1.png)
我想在宏中使用分段命令来增强它们,例如在标题之前放置一些行,在标题之后放置一些内容。
是否可以使用/寻址宏中原始命令的可选参数,例如\subsection
?
平均能量损失
\documentclass[
11pt,
a4paper,
]
{scrartcl}
\usepackage{
lmodern,
blindtext
}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{
multicol,
xcolor,
}
\newcommand{\specialheadline}[1]{%
{\centering
\color{gray}\rule{0.9\columnwidth}{1pt}\color{black}\par}
\subsection{#1}}
\listfiles
\begin{document}
\begin{center}
Title stuff
\end{center}
\begin{multicols*}{2}
\section{Word}
\specialheadline{ASDF}
%\specialheadline[123]{ASDF} -- THIS LINE DOES NOT WORK
\blindtext[1]
\end{multicols*}
\end{document}
答案1
以下定义可能更好,因为它不需要切换回black
,因为您可以在前面使用不同的颜色\specialheadline
:
\newcommand{\specialheadline}[2][]{%
{\centering
\textcolor{gray}{\rule{0.9\columnwidth}{1pt}}\par}
% How to check if a macro value is empty or will not create text with plain TeX conditionals?
% https://tex.stackexchange.com/a/53091/5764
\if\relax\detokenize{#1}\relax
\subsection{#2}%
\else
\subsection[#1]{#2}%
\fi}
它检查提供的可选参数是否\specialheadline
为空,并设置适当的分段单元的条件。您可以将显式条件缩短为如下内容:
\subsection[\if\relax\detokenize{#1}\relax #2\else #1\fi]{#2}
因为\if
...\fi
将根据 ToC 的需要进行扩展。
更可靠的选择是使用xparse
带有条件的命令定义\IfValueTF
:
% \usepackage{xparse}% If you have LaTeX2e < 2020-10
\NewDocumentCommand{\specialheadline}{ o m }{%
{\centering
\textcolor{gray}{\rule{0.9\columnwidth}{1pt}}\par}
% How to check if a macro value is empty or will not create text with plain TeX conditionals?
% https://tex.stackexchange.com/a/53091/5764
\subsection[\IfValueTF{#1}{#1}{#2}]{#2}}