[Tex/LaTex] Boolean variables in LaTeX

conditionalsoptional arguments

I am trying to use "boolean" variables in LaTeX but am unable to have any success…

I have a macro which takes 5 arguments, the last argument being optional. This macro is named educationItem and it only ever appears in an education environment. So, some educationItem entries will have this fifth argument, and some will not. The ones that have the fifth argument will require a slightly modified formatting.

In addition, I would like the ability to disable this fifth argument for all educationItem entries, such that it appears as if all educationItem entries have no fifth argument. In order to do this, I am currently using an option passed when including my cvtools.sty file.

My current attempt (shown below) does not seem to work and stuffs up the table alignment. I think that it may be due to the time at which the various expressions are evaluated, but have not been able to fix it.

Table misalignment

Any help would be appreciated.

A minimum working example follows:

main.tex:

\documentclass[10pt]{article}

\usepackage{cvtools} % my CV styling stuff
\usepackage[margin=3cm]{geometry}   % extend page margins

\begin{document}

\section*{Education}
\begin{education}

\educationItem{2008}{2012}
{Education 1}
{University 1}
{}

\educationItem{2002}{2007}
{Education 2}
{University 2}
{
\educationGrade{Subject 1}{90/100}
\educationGrade{Subject 2}{88/100}
\educationGrade{Subject 3}{97/100}
\educationGrade{Subject 4}{94/100}
\educationGrade{Subject 5}{95/100}
}
\end{education}

\end{document}

cvtools.sty:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{cvtools}[2012/07/16 v1.0.0]

\RequirePackage{etoolbox}           % for \ifstrempty, \ifdef
\RequirePackage{ifthen}             % for \ifthenelse
\RequirePackage{longtable}          % for tables that span more than one page
\RequirePackage{multirow}           % for table columns that span more than one row
\RequirePackage{tabularx}           % for simple column stretching
\RequirePackage{xcolor}             % for colours

%================================================%
% Declare options
%================================================%
\newif\if@showgrades

\DeclareOption{showgrades}{\@showgradestrue}
\DeclareOption*{\PackageWarning{cvtools}{Unknown option `\CurrentOption'}}

\ProcessOptions

%================================================%
% For table styling
%================================================%
\definecolor{lightgray}{gray}{0.8}
\newcolumntype{L}[1]{>{\raggedleft}p{#1}}
\newcolumntype{R}[1]{p{#1}}
\newcommand\VRule{\color{lightgray}\vrule width 0.5pt}
%------------------------------------------------%

%================================================%
% \begin{education}
% \educationItem{beginYear}{endYear}{name}{institution}{\educationGrade{subject}{grade} ...}
% \end{education}
%================================================%
\newenvironment{education}%
{%
\def\lwidth{0.16\textwidth}%
\def\rwidth{0.78\textwidth}%
%
\newcommand\educationGrade[2]{##1 & ##2 \\}%
%
\newcommand\educationItem[5]{%
% Check if we should display tools
\if@showgrades
    \ifstrempty{##5}%
        {% showGrades is true, but this \educationItem has no grades to display.
            \def\@numRows{1}%
            \def\@shouldshowgrades{0}
        }%
        {% showGrades is true and this \educationItem has grades to display.
            \def\@numRows{2}%
            \def\@shouldshowgrades{1}
        }%
\else
    % showGrades is false.
    \def\@numRows{1}%
    \def\@shouldshowgrades{0}
\fi
%
% Education period
\multirow{\@numRows}{\lwidth}{##1 -- ##2}%
%
% Name and institution
& \textbf{##3 (##4)}%
%
% Grades
\ifthenelse{\equal{@shouldshowgrades}{1}}%
    {%
        \\* %
        & \begin{tabularx}{\rwidth}{Xr} %
        ##5 %
        \end{tabularx} %
    }%
    {}%
\\[0.5em]%
}%
%
\begin{longtable}{L{\lwidth}!{\VRule}R{\rwidth}}%
}
{\end{longtable}}
%------------------------------------------------%

The expected output would be identical to the following TeX code… when using \usepackage[showgrades]{cvtools}:

\documentclass[10pt]{article}

\usepackage[margin=3cm]{geometry}   % extend page margins
\RequirePackage{longtable}          % for tables that span more than one page
\RequirePackage{multirow}           % for table columns that span more than one row
\RequirePackage{tabularx}           % for simple column stretching
\RequirePackage{xcolor}             % for colours

%================================================%
% For table styling
%================================================%
\definecolor{lightgray}{gray}{0.8}
\newcolumntype{L}[1]{>{\raggedleft}p{#1}}
\newcolumntype{R}[1]{p{#1}}
\newcommand\VRule{\color{lightgray}\vrule width 0.5pt}
%------------------------------------------------%

\begin{document}

\section*{Education}
% \begin{education}
\def\lwidth{0.16\textwidth}
\def\rwidth{0.78\textwidth}
\begin{longtable}{L{\lwidth}!{\VRule}R{\rwidth}}

% \educationItem{2008}{2012}
% {Education 1}
% {University 1}
% {}
\multirow{1}{\lwidth}{2008 -- 2012}
& \textbf{Education 1 (University 1)}%
% @shouldshowgrades should be false here
\\[0.5em]

% \educationItem{2002}{2007}
% {Education 2}
% {University 2}
% {
% ...
% }
\multirow{1}{\lwidth}{2002 -- 2007}
& \textbf{Education 2 (University 2)}%
% @shouldshowgrades should be true here
\\*
& \begin{tabularx}{\rwidth}{Xr} %
% \educationGrade{Subject 1}{90/100}
Subject 1 & 90/100 \\
% \educationGrade{Subject 2}{88/100}
Subject 2 & 88/100 \\
% \educationGrade{Subject 3}{97/100}
Subject 3 & 97/100 \\
% \educationGrade{Subject 4}{94/100}
Subject 4 & 94/100 \\
% \educationGrade{Subject 5}{95/100}
Subject 5 & 95/100 \\
\end{tabularx}
\\[0.5em]

% \end{education}
\end{longtable}

\end{document}

When using \usepackage{cvtools}:

\documentclass[10pt]{article}

\usepackage[margin=3cm]{geometry}   % extend page margins
\RequirePackage{longtable}          % for tables that span more than one page
\RequirePackage{multirow}           % for table columns that span more than one row
\RequirePackage{tabularx}           % for simple column stretching
\RequirePackage{xcolor}             % for colours

%================================================%
% For table styling
%================================================%
\definecolor{lightgray}{gray}{0.8}
\newcolumntype{L}[1]{>{\raggedleft}p{#1}}
\newcolumntype{R}[1]{p{#1}}
\newcommand\VRule{\color{lightgray}\vrule width 0.5pt}
%------------------------------------------------%

\begin{document}

\section*{Education}
% \begin{education}
\def\lwidth{0.16\textwidth}
\def\rwidth{0.78\textwidth}
\begin{longtable}{L{\lwidth}!{\VRule}R{\rwidth}}

% \educationItem{2008}{2012}
% {Education 1}
% {University 1}
% {}
\multirow{1}{\lwidth}{2008 -- 2012}
& \textbf{Education 1 (University 1)}%
% @shouldshowgrades should be false here
\\[0.5em]

% \educationItem{2002}{2007}
% {Education 2}
% {University 2}
% {
% \educationGrade{Subject 1}{90/100}
% \educationGrade{Subject 2}{88/100}
% \educationGrade{Subject 3}{97/100}
% \educationGrade{Subject 4}{94/100}
% \educationGrade{Subject 5}{95/100}
% }
\multirow{1}{\lwidth}{2002 -- 2007}
& \textbf{Education 2 (University 2)}%
% @shouldshowgrades should be true here
\\[0.5em]

% \end{education}
\end{longtable}

\end{document}

Best Answer

The following with \usepackage[showgrades]{cvtools} produces:

enter image description here

or with \usepackage{cvtools}:

enter image description here

Changes:

  • Added package option to \usepackage[showgrades]{cvtools} so that showgrades was enabled.

  • Few spurious spaces eliminated by adding a % (noted in code).

  • You were missing a \ in:

      \ifthenelse{\equal{\@shouldshowgrades}{1}}
    
  • Changed \def to \gdef so that those settings are available when needed.

  • Eliminated the \multirow for displaying the years.

Code:

\documentclass[10pt]{article}

\usepackage{filecontents}
\begin{filecontents*}{cvtools.sty}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{cvtools}[2012/07/16 v1.0.0]

\RequirePackage{etoolbox}           % for \ifstrempty, \ifdef
\RequirePackage{ifthen}             % for \ifthenelse
\RequirePackage{longtable}          % for tables that span more than one page
\RequirePackage{multirow}           % for table columns that span more than one row
\RequirePackage{tabularx}           % for simple column stretching
\RequirePackage{xcolor}             % for colours

%================================================%
% Declare options
%================================================%
\newif\if@showgrades

\DeclareOption{showgrades}{\@showgradestrue}
\DeclareOption*{\PackageWarning{cvtools}{Unknown option `\CurrentOption'}}

\ProcessOptions

%================================================%
% For table styling
%================================================%
\definecolor{lightgray}{gray}{0.8}%
\newcolumntype{L}[1]{>{\raggedleft}p{#1}}%
\newcolumntype{R}[1]{p{#1}}%
\newcommand\VRule{\color{lightgray}\vrule width 0.5pt}%
%------------------------------------------------%

%================================================%
% \begin{education}
% \educationItem{beginYear}{endYear}{name}{institution}{\educationGrade{subject}{grade} ...}
% \end{education}
%================================================%
\newenvironment{education}%
{%
\def\lwidth{0.16\textwidth}%
\def\rwidth{0.78\textwidth}%
%
\newcommand\educationGrade[2]{##1 & ##2\\}%
%
\newcommand\educationItem[5]{%
% Check if we should display tools
\if@showgrades%
    \ifstrempty{##5}%
        {% showGrades is true, but this \educationItem has no grades to display.
            \gdef\@numRows{1}%
            \gdef\@shouldshowgrades{0}% <-- added %
        }%
        {% showGrades is true and this \educationItem has grades to display.
            \gdef\@numRows{2}%
            \gdef\@shouldshowgrades{1}% <-- added %
        }%
\else%
    % showGrades is false.
    \gdef\@numRows{1}%
    \gdef\@shouldshowgrades{0}% <-- added %
\fi%
%
% Education period
%\multirow{\@numRows}{\lwidth}{##1 -- ##2}%
##1 -- ##2% <-- Eliminated \multirow 

%
% Name and institution
& \textbf{##3 (##4)}%
%
% Grades
\ifthenelse{\equal{\@shouldshowgrades}{1}}% <-- corrected (missing \)
    {%
        \\*%
        & \begin{tabularx}{\rwidth}{Xr}%
        ##5%
        \end{tabularx}%
    }%
    {}%
\\[0.5em]%
}%
%
\begin{longtable}{L{\lwidth}!{\VRule}R{\rwidth}}%
}%
{\end{longtable}}%
\end{filecontents*}


\usepackage[showgrades]{cvtools}% <-- added package option
\usepackage[margin=3cm]{geometry}   % extend page margins

\begin{document}
\section*{Education}
\begin{education}
    \educationItem{2008}{2012}%
    {Education 1}%
    {University 1}%
    {}%
    %
    \educationItem{2002}{2007}%
    {Education 2}%
    {University 2}%
    {%
    \educationGrade{Subject 1}{90/100}%
    \educationGrade{Subject 2}{88/100}%
    \educationGrade{Subject 3}{97/100}%
    \educationGrade{Subject 4}{94/100}%
    \educationGrade{Subject 5}{95/100}%
    }
\end{education}
\end{document}