[Tex/LaTex] LaTeX “Variables” – \@varname

conditionalsdocumentclass-writingmacros

I'm currently in the process of writing my first document class file (a personalized resume class), and I want to get a better understanding of what exactly I'm doing. So, right now I'm setting up commands that allow for values to be assigned to variables (not sure if that's really the word I should be using), through a structure like this:

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

So, in the .tex file, the user can use the command \institution{University of Whatever} to save the string "University of Whatever" to \@institution, which is then later called within the class file by another command.

All of this works as I want it to, but now I'm hoping to create some conditional expressions to control the output. Like, I have a command \education that when called in the document will format an education section for a resume given the institution name, dates attended, degree info, etc. that the user had already entered. I want to be able to set it up in the class file to check if these \@variable variables have been defined, and then format the output differently based on which are defined and which are empty.

Primarily, I think a lot of my problem is that I don't actually understand what the \@variable definitions are or the scope of what I can do with them.

A full example of what I'm trying to achieve would be along the lines of (in LaTeX/pseudo):

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

So, if \@datesattended were defined, the formatting would change to accommodate it. Otherwise, the command would just pass over it, printing the information that was given.

Best Answer

There is nothing special about \@variable commands. They are just macros, for storing content rather than performing other operations. As such it's possible to test for being defined, by using \ifdefined, a (e-TeX) primitive.

\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}

Edit The same should be achievable using \ifdef from etoolbox package

Related Question