
我定義了兩個新的巨集和命令,以使用文件中的以下命令更改其內容.cls
。
\newcommand{\@mymacroa}{}
\newcommand{\mymacroa}[1]{\renewcommand{\@mymacroa}{#1}}
\newcommand{\@mymacrob}{}
\newcommand{\mymacrob}[1]{\renewcommand{\@mymacrob}{#1}}
現在我想測試其中之一(或兩者)是否非空(即已從預設值修改),如果其中之一有內容,則列印它們的值。我之前嘗試使用\ifthenelse
,\@ifnotmtarg
和 plain TeX\if
都沒有成功。
在偽代碼中,我想要實現的是:
\@ifnotmtarg{\@mymacroa}\or\@ifnotmtarg{\@mymacrob}
do {print some text and the macros which are non-empty}
答案1
如果不知道這些數據將在包中以什麼其他方式使用,很難說最好的方法,特別是因為我不熟悉家譜樹包裹。
考慮到這一點,一種方法是定義命令,例如\DateOfBirth
和\PlaceOfBirth
,分別設定出生日期和地點,可以將其保存在巨集中,例如\@dateofbirth
和\@placeofbirth
。透過將這兩個巨集的預設值設為,\relax
您現在可以測試它們是否已透過定義新的布林值(例如 )\ifHaveDateOrPlace
並使用類似以下內容來重新定義:
% include the date and/or place of birth if available
\HaveDateOrPlacefalse% reset date and place boolean
\if\@dateofbirth\relax\else\HaveDateOrPlacetrue\fi
\if\@placeofbirth\relax\else\HaveDateOrPlacetrue\fi
\ifHaveDateOrPlace\gtrsymBorn \@placeofbirth \@dateofbirth \fi
當然,您還需要巨集來獲取圖像檔案的名稱以及可能的人名,但也許這些已經由家譜樹。有了這個,您就可以定義一個宏\MugShot
,以便程式碼
\MugShot
\PlaceOfBirth{Mars}
\MugShot
\DateOfBirth{Tuesday}
\MugShot
\PlaceOfBirth{Venus}
\DateOfBirth{Wednesday}
\MugShot
會產生:
(我的預設圖片是一個問號。)
該 MWE 的完整代碼由乳膠文件組成:
\documentclass{myclass}
\begin{document}
\MugShot
\PlaceOfBirth{Mars}
\MugShot
\DateOfBirth{Tuesday}
\MugShot
\PlaceOfBirth{Venus}
\DateOfBirth{Wednesday}
\MugShot
\end{document}
myclass.cls
與包含所有內容的類別文件一起:
\LoadClass[12pt]{amsart}
\RequirePackage{genealogytree}
\RequirePackage{graphicx}
% boolean to keep track of place and date of birth
\newif\ifHaveDateOrPlace
% place of birth
\providecommand\@placeofbirth{\relax}
\newcommand\PlaceOfBirth[1]{\renewcommand\@placeofbirth{\space#1}}
% date of birth
\providecommand\@dateofbirth{\relax}
\newcommand\DateOfBirth[1]{\renewcommand\@dateofbirth{\space#1}}
\providecommand\@personpicture{{\Huge?}}
\newcommand\Picture[2][]{\edef\@personpicture{\noexpand\includegraphics[width=30mm,#1]{#2}}}
% reset the people data
\newcommand\ResetData{%
\renewcommand\@placeofbirth{\relax}%
\renewcommand\@dateofbirth{\relax}%
}
\newcommand\MugShot{%
\begin{tabular}{c}
\@personpicture\\
% include the date and/or place of birth if available
\HaveDateOrPlacefalse% reset date and place boolean
\if\@dateofbirth\relax\else\HaveDateOrPlacetrue\fi
\if\@placeofbirth\relax\else\HaveDateOrPlacetrue\fi
\ifHaveDateOrPlace\gtrsymBorn \@placeofbirth \@dateofbirth \fi
\end{tabular}%
\ResetData
}
\endinput
如果此數據僅使用一次,那麼更好的方法可能是定義一個巨集來列印照片以及相關數據,或使用類似的方法pgf鍵這樣您就可以使用鍵值語法來指定所有內容。