2つのマクロのうち1つが空でないかどうかをテストする

2つのマクロのうち1つが空でないかどうかをテストする

次のコマンドを使用して、2 つの新しいマクロとコマンドを定義し、その内容を変更します.cls

\newcommand{\@mymacroa}{}
\newcommand{\mymacroa}[1]{\renewcommand{\@mymacroa}{#1}}
\newcommand{\@mymacrob}{}
\newcommand{\mymacrob}[1]{\renewcommand{\@mymacrob}{#1}}

ここで、これらのうちの 1 つ (または両方) が空でない (つまり、デフォルト値から変更されている) かどうかをテストし、これらのうちの 1 つにコンテンツがある場合は、その値を出力します。、\ifthenelseおよび\@ifnotmtargプレーン TeXを使用した以前の試みは\if成功しませんでした。

疑似コードで実現したいことは次のとおりです。

\@ifnotmtarg{\@mymacroa}\or\@ifnotmtarg{\@mymacrob} 
do {print some text and the macros which are non-empty}

答え1

このデータがパッケージ内でどのように使用されるかを知らずにこれを行う最善の方法を言うのは難しいです。特に、系図パッケージ。

この注意点を踏まえると、これを行う 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 の完全なコードは、次の LaTeX ファイルで構成されています。

\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キーキーと値の構文を使用してすべてを指定できるようにします。

関連情報