我想在文档中添加一些条件段落。我一直在遵循“按主题分类”,但我遇到了限制。
\documentClass{report}
\def\option{a}
\begin{document}
\if\option a
This is a paragraph related to option A
\fi
\end{document}
但是,我的选项的值仅限于一个字符。如何使用\if
来匹配多字符字符串?我尝试了以下几种方法,但始终无法匹配
\documentClass{report}
\def\brand{toyota}
\begin{document}
\if\brand toyota
This paragraph is specific to toyota
\else \if\brand{volvo}
This paragraph is specific to volvo
\else
This paragraph is generic
\fi
\end{document}
答案1
尝试一下:
\documentclass{report}
\def\brand{toyota}
\def\brandToyota{toyota}
\def\brandVolvo{volvo}
\begin{document}
\ifx\brand\brandToyota
This paragraph is specific to toyota
\else \ifx\brand\brandVolvo
This paragraph is specific to volvo
\else
This paragraph is generic
\fi
\fi
\end{document}
\ifx
只需在扩展命令后进行比较即可。
答案2
您可以使用字符串开关:
\documentclass{article}
\ExplSyntaxOn
\NewDocumentCommand{\stringswitch}{mmm}
{
\str_case_e:nnF { #1 } { #2 } { #3 }
}
\ExplSyntaxOff
\newcommand{\brand}{toyota}
\begin{document}
\stringswitch{\brand}{
{toyota}{This paragraph is specific to toyota}
{volvo}{This paragraph is specific to volvo}
}{This paragraph is generic}
\renewcommand{\brand}{volvo}
\stringswitch{\brand}{
{toyota}{This paragraph is specific to toyota}
{volvo}{This paragraph is specific to volvo}
}{This paragraph is generic}
\renewcommand{\brand}{cadillac}
\stringswitch{\brand}{
{toyota}{This paragraph is specific to toyota}
{volvo}{This paragraph is specific to volvo}
}{This paragraph is generic}
\end{document}
答案3
您可以\ifdefstring
使用etoolbox
:
\documentClass{report}
\def\brand{toyota}
\begin{document}
\ifdefstring{\brand}{toyota}{
This paragraph is for Toyota owners
}{\ifdefstring{\brand}{volvo}{
If you're a Volvo owner, you'll want to read this paragraph.
}{
This paragraph is for everyone else
}
\end{document}
答案4
如果您坚持使用\if
,David Kastrup 的\if\strequal...
-检验可能会引起您的兴趣。
(修改下面的代码以便\def
使用 LaTeX 的定义命令并且不会与 etoolbox 等包的宏发生名称冲突,这可能是一项不错的家庭作业。)
使用(普通)TeX 而不是 LaTeX 编译以下示例。
%%======================================================================
%% Expandable comparison of two strings:
%%
%% David Kastrup's \if\strequal-test from the
%% TeX Pearl Diving Site;
%% Pearls of 2005;
%% Title: David Kastrup - Comparing two strings known to consist
%% only of characters ;
%% Url: <http://www.gust.org.pl/projects/pearls/2005p/david-kastrup/bachotex2005-david-kastrup-pearl1.pdf>
%%----------------------------------------------------------------------
%% Usage:
%% \if\strequal{junk}{#1}true\else false\fi
%% will be true for #1 being “junk”, and false otherwise
%%......................................................................
\def\strequal#1{\number\strequalstart{}{}#1\relax}%
\def\strequalstart#1#2#3{\if#3\relax\strequalstop\fi\strequalstart{\if#3#1}{#2\fi}}%
\def\strequalstop\fi\strequalstart#1#2#3{\fi#1#3\relax'#213 }%
%%======================================================================
%% Expandable comparison of two strings known to expand to characters
%% only; \strcmp
%% - expands strings via \csname..\endcsname and applies \string
%% - and replaces explicit space tokens with category-12 space tokens,
%% - then does David Kastrup's \strequal-test.
%%----------------------------------------------------------------------
%% Usage:
%% \if\strcmp{ju nk}{#1}true\else false\fi
%% will be true for #1 expanding “ju nk”, and false otherwise
%%......................................................................
\def\strfot#1#2{#1}\def\strsot#1#2{#2}\long\def\strexchg#1#2{#2{#1}}%
\long\def\strmakecstoken#1{%
\expandafter\expandafter\expandafter\strexchg
\expandafter\expandafter\expandafter{\expandafter\string\csname #1\endcsname}%
}%
\def\strspacereplace#1{%
\if\relax\strgobbletospace#1 \relax\expandafter\strfot\else\expandafter\strsot\fi
{\strexchg{#1}}{\expandafter\strspacereplace\expandafter{\strdorepl#1}}%
}%
\def\strgobbletospace#1 {}%
\begingroup
\def\strdorepl#1{\endgroup\def\strdorepl##1 {##1#1}}%
\catcode`\ =12\relax
\strdorepl{ }%
\long\def\strcmp#1#2{%
\strmakecstoken{#2}{\strspacereplace}%
{\strmakecstoken{#1}{\strspacereplace}{\strequal}}%
}%
%%======================================================================
\def\brand{toyota}
%\def\brand{vol vo}
%\def\brand{generic}
This paragraph is
\if\strcmp{\brand}{toyota}%
specific to toyota%
\else
\if\strcmp{\brand}{vol vo}%
specific to volvo%
\else
generic%
\fi
\fi
.
\bye