Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
Can you provide what the expected output is. –  Peter Grill Jul 17 '12 at 4:23
    
@PeterGrill - added LaTeX code that would produce identical expected output –  Joshua Spence Jul 17 '12 at 4:53
1  
Follow up question: Misalignment of table columns when using multirow. –  Peter Grill Jul 18 '12 at 21:26

1 Answer 1

up vote 5 down vote accepted

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}
share|improve this answer
    
Great! Thanks Peter. However, I would like to use the multirow there... the reason is that I do a similar thing in another environment with longer text in the left column. Without multirow the second row is shifted down to make room for the text in the upper left cell... hence why the multirow is required. Why is it that doing it your way (but using the multirow), the left and right columns become out of alignment? –  Joshua Spence Jul 18 '12 at 3:36
    
@JoshuaSpence: I am not sure what is going on with the \multirow. I would suggest posting a follow up question specifically for the \multirow issue, with this MWE as it is already packaged, and reference that that is a follow up to this question. –  Peter Grill Jul 18 '12 at 4:39
    
Good idea. Thanks –  Joshua Spence Jul 18 '12 at 5:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.