LaTeX「変数」 - \@varname

LaTeX「変数」 - \@varname

現在、最初のドキュメント クラス ファイル (パーソナライズされた履歴書クラス) を作成中ですが、自分が何をしているのかをより深く理解したいと考えています。そこで、現在、次のような構造を使用して、変数 (この言葉を使うべきかどうかはわかりません) に値を割り当てることができるコマンドを設定しています。

\newcommand{\institution}[1]{\def\@institution{#1}}
\newcommand{\datesattended}[1]{\def\@datesattended{#1}}
\newcommand{\degree}[1]{\def\@degree{#1}}

したがって、.tex ファイルでは、ユーザーは コマンドを使用して\institution{University of Whatever}文字列「University of Whatever」を に保存し\@institution、後でクラス ファイル内で別のコマンドによって呼び出すことができます。

これらはすべて期待どおりに動作しますが、今度は出力を制御するための条件式をいくつか作成したいと考えています。たとえば、\educationドキュメント内で呼び出されると、ユーザーがすでに入力した教育機関名、在籍日、学位情報などに基づいて履歴書の教育セクションをフォーマットするコマンドがあります。クラス ファイルでこれを設定して、これらの\@variable変数が定義されているかどうかを確認し、定義されているものと空なものに基づいて出力を異なる方法でフォーマットできるようにしたいと考えています。

\@variable第一に、私の問題の多くは、定義が何であるか、または定義を使用して何ができるかの範囲を実際に理解していないことにあると思います。

私が実現しようとしていることの完全な例は、次のようになります (LaTeX/疑似)。

\newcommand{\showeducation}{%
    \@institutionname -- \@degree
    if \@datesattended is defined:
        \newline \@datesattended
    clear \@institutionname, \@datesattended, \@degree
}

したがって、\@datesattended定義されている場合は、それに合わせてフォーマットが変更されます。定義されていない場合は、コマンドはそれを無視して、指定された情報を出力します。

答え1

コマンドには特別な点はありません。コマンドは、他の操作を実行するのではなく、コンテンツを格納するための単なるマクロです。そのため、(e-TeX) プリミティブ\@variableを使用して、定義されているかどうかをテストすることができます 。\ifdefined

\documentclass[11pt,a4paper]{article}


\makeatletter

\newcommand{\@institutionname}{Ministry of Silly Walks}
\newcommand{\@degree}{Minster of Silly Walks}

%\newcommand{\@datesattended}{1969}


\newcommand{\showeducation}{%
    \@institutionname\ -- \@degree
    \ifdefined\@datesattended 
        \newline \@datesattended  % Please use some 'better' setup here
        \else
     \let\@institutionname\relax
     \let\@datesattended\relax 
     \let\@degree\relax
     \fi
}

\makeatother


\begin{document}

\showeducation   % Date should not be printed

\makeatletter
\newcommand{\@datesattended}{1969}
\makeatother

\showeducation % Now it should be printed, but the rest is \relax`ed


\end{document}

編集同じことはパッケージ\ifdefから使用することで達成できるはずですetoolbox

答え2

\newcommand[1]...おそらく、 toks レジスタの使用法の方が優れています。

\newtoks\institution  \newtoks\datesattended  \newtoks\degree

ユーザーが言うと

\institution{Ministry of Silly Walks}

この値をマクロ内で次のように使用できます。

\the\institution

「変数」の値がすでに設定されているかどうかをテストする必要がある場合は、次のようにします。

\if\relax\the\degree\relax The degree isn't set.\else The degree is set.\fi

関連情報