我使用不同的 if 条件组合成一个名称。如果所有 if 条件都为假,并且最后一个条件为真,则该名称有效。如果第一个条件为真,则会出现错误,Incomplete \iftrue; all text was ignored after line 22.
我真的不知道哪里出了问题。
编辑:我尝试为 vonname 添加一个条件,但似乎不起作用……它说命令\@name
未定义。我相应地更改了下面的代码。
\documentclass{article}
\makeatletter
% Name display:
% Include vonname:
\newif\if@displayvon
%\DeclareOption{vonname=true}{\@displayvontrue}
% Displaystyles:
\newif\if@lastfirstvonname
%\DeclareOption{namedisplay=lastfirstvon}{\@lastfirstvonnametrue}
%\DeclareOption{namedisplay=lastfirst}{\@lastfirstvonnametrue}
\newif\if@vonlastfirstname
%\DeclareOption{namedisplay=vonlastfirst}{\@vonlastfirstnametrue}
%\let\if@lastfirstvonname\iftrue
\newcommand{\firstname}[1]{\def\@firstname{#1}}
\newcommand{\vonname}[1]{\def\@vonname{#1}}
\newcommand{\lastname}[1]{\def\@lastname{#1}}
%\newcommand{\@name}{\@firstname\ \@vonname\ \@lastname}
\if@lastfirstvonname
\if@displayvon
\def\@name{\@lastname,\ \@firstname\ \@vonname}
\else
\def\@name{\@lastname,\ \@firstname}
\else
\if@vonlastfirstname
\if@displayvon
\def\@name{\@vonname\ \@lastname,\ \@firstname}
\else
\def\@name{\@lastname,\ \@firstname}
\else
\if@displayvon
\def\@name{\@firstname\ \@vonname\ \@lastname}
\else
\def\@name{\@firstname\ \@lastname}
\fi\fi\fi\fi\fi
\makeatother
\firstname{First}
\vonname{Von}
\lastname{Last}
\begin{document}
hello, my name is \par
\makeatletter
\@name
\makeatother
\end{document}
答案1
考虑在结尾处也使用\if
...\else
代码缩进\fi
。这样应该可以清楚地表明条件\if
... \else
...\fi
结构的开始和结束位置。
以下是该结构的视觉效果:
\if@lastfirstvonname % <--------------------------------------------------IF---\
\if@displayvon % <------------------------------------IF---\ |
\def\@name{\@lastname,\ \@firstname\ \@vonname}% | |
\else % <---------------------------------------------ELSE--| |
\def\@name{\@lastname,\ \@firstname}% | |
\fi % <-----------------------------------------------FI---/ |
\else % <-----------------------------------------------------------------ELSE--|
\if@vonlastfirstname % <---------------------------------------IF---\ |
\if@displayvon % <----------------------------------IF---\ | |
\def\@name{\@vonname\ \@lastname,\ \@firstname}% | | |
\else % <-------------------------------------------ELSE--| | |
\def\@name{\@lastname,\ \@firstname}% | | |
\fi % <---------------------------------------------FI---/ | |
\else % <------------------------------------------------------ELSE--| |
\if@displayvon % <----------------------------------IF---\ | |
\def\@name{\@firstname\ \@vonname\ \@lastname}% | | |
\else % <-------------------------------------------ELSE--| | |
\def\@name{\@firstname\ \@lastname}% | | |
\fi % <---------------------------------------------FI --/ | |
\fi % <--------------------------------------------------------FI---/ |
\fi % <-------------------------------------------------------------------FI---/
您使用的一组结束符\fi
没有正确关闭组,导致某些\if
子句没有相应的\fi
结束符。
答案2
你有三个 \if ...你也需要三个的原因\fi
:
\if@lastfirstvonname
\newcommand{\@name}{\@lastname,\ \@firstname\ \@vonname}
\else
\if@vonlastfirstname
\newcommand{\@name}{\@vonname\ \@lastname,\ \@firstname}
\else
\if@firstvonlastname
\newcommand{\@name}{\@firstname\ \@vonname\ \@lastname}
\else
\newcommand{\@name}{\@firstname\ \@vonname\ \@lastname}
\fi\fi\fi
或者
\if@lastfirstvonname
\if@displayvon
\def\@name{\@lastname,\ \@firstname\ \@vonname}
\else
\def\@name{\@lastname,\ \@firstname}
\fi
\else
\if@vonlastfirstname
\if@displayvon
\def\@name{\@vonname\ \@lastname,\ \@firstname}
\else
\def\@name{\@lastname,\ \@firstname}
\fi
\else
\if@displayvon
\def\@name{\@firstname\ \@vonname\ \@lastname}
\else
\def\@name{\@firstname\ \@lastname}
\fi
\fi
\fi
答案3
你的顺序\if… … \else … \fi
是错误的,例如这里:
\if@lastfirstvonname
\if@displayvon
\def\@name{\@lastname,\ \@firstname\ \@vonname}
\else
\def\@name{\@lastname,\ \@firstname}
\else
你有\if… … \else … \else
,这是不允许的。
所以我认为你想要这样的东西:
\documentclass{article}
\makeatletter
% Name display:
% Include vonname:
\newif\if@displayvon
%\DeclareOption{vonname=true}{\@displayvontrue}
% Displaystyles:
\newif\if@lastfirstvonname
%\DeclareOption{namedisplay=lastfirstvon}{\@lastfirstvonnametrue}
%\DeclareOption{namedisplay=lastfirst}{\@lastfirstvonnametrue}
\newif\if@vonlastfirstname
%\DeclareOption{namedisplay=vonlastfirst}{\@vonlastfirstnametrue}
\newcommand{\firstname}[1]{\def\@firstname{#1}}
\newcommand{\vonname}[1]{\def\@vonname{#1}}
\newcommand{\lastname}[1]{\def\@lastname{#1}}
\if@lastfirstvonname% outer \if… … \else … \fi started
\if@displayvon% inner \if… … \else … \fi started
\def\@name{\@lastname,\ \@firstname\ \@vonname}
\else% else of inner \if… … \else … \fi
\def\@name{\@lastname,\ \@firstname}
\fi% inner \if… … \else … \fi finished
\else
\if@vonlastfirstname% middle \if… … \else … \fi started
\if@displayvon% inner \if… … \else … \fi started
\def\@name{\@vonname\ \@lastname,\ \@firstname}
\else% else of inner \if… … \else … \fi
\def\@name{\@lastname,\ \@firstname}
\fi% inner \if… … \else … \fi finished
\else% else of middle \if… … \else … \fi
\if@displayvon% inner \if… … \else … \fi started
\def\@name{\@firstname\ \@vonname\ \@lastname}
\else% else of inner \if… … \else … \fi
\def\@name{\@firstname\ \@lastname}
\fi % inner \if… … \else … \fi finished
\fi % middle \if… … \else … \fi finished
\fi % outer \if… … \else … \fi finished
\makeatother
\firstname{First}
\vonname{Von}
\lastname{Last}
\begin{document}
hello, my name is \par
\makeatletter
\@name
\makeatother
\end{document}
我已经对所有嵌套的部分进行了评论\if… … \else … \fi
,以使其更加清晰。