使用多行时表格列未对齐

使用多行时表格列未对齐

我有一个 LaTeX 文档,在其中使用自定义环境和命令来使一切变得简单一些。但是,当我在其中一个自定义环境中使用多行命令时,左右表列的对齐变得混乱(见下文)。

表格列错位示例

下面是一个最小工作示例。请注意,在上面显示的情况下,不需要多行。但是,在其他情况下,左列中的文本比左列中的单个表格单元格长,因此需要多行来防止右列的内容被进一步分隔开。

\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}%
        }%
        {% showGrades is true and this \educationItem has grades to display.
            \gdef\@numRows{2}%
            \gdef\@shouldshowgrades{1}%
        }%
\else%
    % showGrades is false.
    \gdef\@numRows{1}%
    \gdef\@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}}%
\end{filecontents*}


\usepackage[showgrades]{cvtools}%
\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}
    }

    \educationItem{January 2002}{December 2007}
    {Education 3}
    {University 3}
    {
    \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}

第三个\educationItem演示了一个更长的条目,(我相信)证明了使用多行是合理的...如果没有多行,我会得到这样的结果(请注意“教育 3”和“主题 1”之间不需要的空格):

宽左列的示例。

任何帮助都将不胜感激。

答案1

只需更换

\multirow{\@numRows}{\lwidth}{##1 -- ##2}%

\smash{\parbox[t]{\lwidth}{\raggedright ##1 -- ##2}}%

这样该条目就不会影响行高。当右侧部分比左侧部分短时,可能会出现问题,但我认为这种情况不会真正发生。在这种情况下,请添加虚拟\educationGrade{}{}项目。


注意。很难理解为什么您要将 \educationItem 的定义放在and \educationGrade的定义主体中education

相关内容