LaTeX 中的布尔变量

LaTeX 中的布尔变量

我尝试在 LaTeX 中使用“布尔”变量,但未能成功......

我有一个宏,它有 5 个参数,最后一个参数是可选的。这个宏是有名字的,educationItem并且只出现在education环境中。所以,有些educationItem条目会有第五个参数,有些则没有。有第五个参数的条目需要稍微修改一下格式。

此外,我希望能够禁用所有educationItem条目的第五个参数,这样看起来好像所有educationItem条目都没有第五个参数。为了做到这一点,我目前使用包含文件时传递的一个选项cvtools.sty

我目前的尝试(如下所示)似乎不起作用,并且搞乱了表格对齐。我认为这可能是由于评估各种表达式的时间所致,但无法修复它。

工作台错位

任何帮助,将不胜感激。

最小工作示例如下:

主要.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}}
%------------------------------------------------%

使用时,预期输出将与以下 TeX 代码相同…… \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}

使用时\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}

答案1

执行下列操作可\usepackage[showgrades]{cvtools}产生:

在此处输入图片描述

或者\usepackage{cvtools}

在此处输入图片描述

变化:

  • 添加了包选项以便启用\usepackage[showgrades]{cvtools}该选项。showgrades

  • 通过添加%(在代码中注明)消除了一些虚假空格。

  • 您遗漏了\

      \ifthenelse{\equal{\@shouldshowgrades}{1}}
    
  • 更改\def\gdef,以便在需要时这些设置可用。

  • 消除了\multirow显示年份的功能。

代码:

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

相关内容