使用 \foreach 循环内的 \ifthenelse 自动填充表格

使用 \foreach 循环内的 \ifthenelse 自动填充表格

你好,我一直在尝试使用 \foreach 循环来填充一个 2 列表格,并且在里面我使用条件来查看是否进行换行:

\newcommand\skillstext[1]{ 
\renewcommand{\skillstext}{
    \begin{tabular}{cc}
        \foreach [count=\i] \x in {#1}{ 
            \ifthenelse{\ifodd\value{\i}{true}}{\x \\}{\x &}
        }
    \end{tabular}
    }
}

然而,这也行不通,我也不知道这是否重要,但在错误消息中,它说它对于 \hbox 来说太大了,我认为这是因为它位于我创建的侧边栏中

更新:示例代码 你好,这就是我正在尝试做的工作:

这是 .tex 代码

    \documentclass[letterpaper]{cvdocex} % a4paper for A4

%----------------------------------------------------------------------------------------
%    PERSONAL INFORMATION
%----------------------------------------------------------------------------------------

% If you don't need one or more of the below, just remove the content leaving the command, e.g. \cvnumberphone{}

\cvname{Name} % Your name
\cvjobtitle{thing} % Job title/career

\stident{st}
\cvdate{\today} % Date of birth
\cvaddress{england} % Short address/location, use \newline if more than 1 line is required
\cvnumberphone{999 999 9999} % Phone number
\cvsite{}
\cvmail{} % Email address

%----------------------------------------------------------------------------------------

\begin{document}

%----------------------------------------------------------------------------------------
%    Creation de side bar
%----------------------------------------------------------------------------------------
%----------------------------------------------------------------------------------------
%    À propos de moi
%----------------------------------------------------------------------------------------

\aboutme{}

%----------------------------------------------------------------------------------------
%    SKILLS
%----------------------------------------------------------------------------------------
% Skill definitions

% Skill bar section, each skill must have a value between 0 an 6 (float)
\skills{{Relations Public/4},{MATLAB/5},{CATIA V5/5},{VBA/5.5}}

%------------------------------------------------

% Skill text section
\skillstext{{LaTeX},{SQL},{Suite MS Office},{Leadership},{cohésion d'équipe},{C++}}

%----------------------------------------------------------------------------------------

\makeprofile % Print the sidebar

\end{document}

以下是 .cls 代码(其中 skillstext 命令):

\ProvidesClass{cvdocex}[2015/02/28 CV class]
\LoadClass{article}
\NeedsTeXFormat{LaTeX2e}

%----------------------------------------------------------------------------------------
%    REQUIRED PACKAGES
%----------------------------------------------------------------------------------------

\RequirePackage[sfdefault]{ClearSans}
\RequirePackage[T1]{fontenc}
\RequirePackage{tikz}
\RequirePackage{fontawesome}
\RequirePackage{xcolor}
\RequirePackage[absolute,overlay]{textpos}
\RequirePackage{enumitem}
\RequirePackage{ragged2e}
\RequirePackage{etoolbox}
\RequirePackage{ifmtarg}
\RequirePackage{ifthen}
\RequirePackage{pgffor}
\RequirePackage{marvosym}
\RequirePackage{parskip}
\RequirePackage{hyperref}

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions\relax

%----------------------------------------------------------------------------------------
%    COLOURS
%----------------------------------------------------------------------------------------

\definecolor{white}{RGB}{255,255,255}
\definecolor{gray}{HTML}{4D4D4D}
\definecolor{sidecolor}{HTML}{E7E7E7}
\definecolor{mainblue}{HTML}{377933}    %actually green
\definecolor{maingray}{HTML}{B9B9B9}

%----------------------------------------------------------------------------------------
%    MISC CONFIGURATIONS
%----------------------------------------------------------------------------------------

\renewcommand{\bfseries}{\color{gray}} % Make \textbf produce coloured text instead

\pagestyle{empty} % Disable headers and footers

\setlength{\parindent}{0pt} % Disable paragraph indentation

%----------------------------------------------------------------------------------------
%    SIDEBAR DEFINITIONS
%----------------------------------------------------------------------------------------

\setlength{\TPHorizModule}{1cm} % Left margin
\setlength{\TPVertModule}{1cm} % Top margin

\newlength\imagewidth
\newlength\imagescale
\pgfmathsetlength{\imagewidth}{5cm}
\pgfmathsetlength{\imagescale}{\imagewidth/600}

\newcommand{\profilesection}[2]{\vspace{8pt}{\color{black!80} \huge #1 \rule[0.15\baselineskip]{#2}{1pt}}}

% Define custom commands for CV info
\newcommand{\stident}[1]{\renewcommand{\stident}{#1}}
\newcommand{\cvdate}[1]{\renewcommand{\cvdate}{#1}}
\newcommand{\cvmail}[1]{\renewcommand{\cvmail}{#1}}
\newcommand{\cvnumberphone}[1]{\renewcommand{\cvnumberphone}{#1}}
\newcommand{\cvaddress}[1]{\renewcommand{\cvaddress}{#1}}
\newcommand{\cvsite}[1]{\renewcommand{\cvsite}{#1}}
\newcommand{\aboutme}[1]{\renewcommand{\aboutme}{#1}}
\newcommand{\profilepic}[1]{\renewcommand{\profilepic}{#1}}
\newcommand{\cvname}[1]{\renewcommand{\cvname}{#1}}
\newcommand{\cvjobtitle}[1]{\renewcommand{\cvjobtitle}{#1}}

% Command for printing the contact information icons
\newcommand*\icon[1]{\tikz[baseline=(char.base)]{\node[shape=circle,draw,inner sep=1pt, fill=mainblue,mainblue,text=white] (char) {#1};}}

% Command for printing skill progress bars
\newcommand\skills[1]{ 
    \renewcommand{\skills}{
        \begin{tikzpicture}
            \foreach [count=\i] \x/\y in {#1}{
                \draw[fill=maingray,maingray] (0,\i) rectangle (6,\i+0.4);
                \draw[fill=white,mainblue](0,\i) rectangle (\y,\i+0.4);
                \node [above right] at (0,\i+0.4) {\x};
            }
        \end{tikzpicture}
    }
}

% Command for printing skills text
\newcommand\skillstext[1]{ 
    \renewcommand{\skillstext}{
        \begin{tabular}{cc}
            \foreach [count=\i] \x in {#1}{ 
                \ifthenelse{\ifodd\value{\i}{true}}{\x \\}{\x &}
            }
        \end{tabular}
    }
}

%----------------------------------------------------------------------------------------
%    SIDEBAR LAYOUT
%----------------------------------------------------------------------------------------

\newcommand{\makeprofile}{
    \begin{tikzpicture}[remember picture,overlay]
        \node [rectangle, fill=sidecolor, anchor=north, minimum width=9cm, minimum height=\paperheight+1cm] (box) at (-5cm,1cm){};
    \end{tikzpicture}

    %------------------------------------------------

    \begin{textblock}{6}(0.5, 0.2)

%       %------------------------------------------------
%       
%       \begin{center}
%           \begin{tikzpicture}[x=\imagescale,y=-\imagescale]
%               \clip (600/2, 567/2) circle (567/2);
%               \node[anchor=north west, inner sep=0pt, outer sep=0pt] at (0,0) {\includegraphics[width=\imagewidth]{\profilepic}};
%           \end{tikzpicture}
%       \end{center}
%
%       %------------------------------------------------
        \vspace{5mm}
        {\Huge\color{mainblue}\cvname}

        %------------------------------------------------

        {\Large\color{black!80}\cvjobtitle}

        %------------------------------------------------

        \renewcommand{\arraystretch}{1.6}
        \begin{tabular}{p{0.5cm} @{\hskip 0.5cm}p{5cm}}
            \ifthenelse{\equal{\stident}{}}{}{\textsc{\icon{\faGraduationCap}} & \stident\\}
            \ifthenelse{\equal{\cvdate}{}}{}{\textsc{\Large\icon{\Info}} & \cvdate\\}
            \ifthenelse{\equal{\cvaddress}{}}{}{\textsc{\Large\icon{\Letter}} & \cvaddress\\}
            \ifthenelse{\equal{\cvnumberphone}{}}{}{\textsc{\Large\icon{\Telefon}} & \cvnumberphone\\}
            \ifthenelse{\equal{\cvsite}{}}{}{\textsc{\Large\icon{\Mundus}} & \href{\cvsite}{\cvsite}\\}
            \ifthenelse{\equal{\cvmail}{}}{}{\textsc{\large\icon{\MVAt}} & \href{mailto:\cvmail}{\cvmail}}
        \end{tabular}

        %------------------------------------------------

        \profilesection{About me}{2.6cm}

        \begin{justify}
            \aboutme
        \end{justify}

        %------------------------------------------------

        \profilesection{Skills}{4cm}

        \skills
        \skillstext
        \scriptsize
        (*)[The skill scale is from 0 (Fundamental Awareness) to 6 (Expert).]

        %------------------------------------------------

    \end{textblock}
}
\RequirePackage[left=7.6cm,top=0.5cm,right=1cm,bottom=0.6cm,nohead,nofoot]{geometry}

抱歉,忘记说了,这是第二十二个 CV 模板类的修改版本。从这里下载:http://www.latextemplates.com/template/twenty-seconds-resumecv 所有荣誉归于作者

答案1

首先,您\value{\i}将引发错误,因为\i不会扩展为 LaTeX 计数器的名称。其次,不要使用\ifthenelse作为类加载etoolbox

\ifnumodd \i {\x \\}{\x &}

但它不起作用,因为\foreach不能以这种方式在表格中使用。事实上,跟踪代码显示您正在尝试执行的操作(在我上面的修复之后):

\skillstext -> \begin {tabular}{cc} \foreach [count=\i ] \x in {{LaTeX},
{SQL},{Suite MS Office},{Leadership},{cohésion d'équipe},{C++}}{\ifnumodd
\i {\x \\}{\x &}} \end {tabular}

这行不通。您需要一个可扩展的循环或一个设计用于表格内部或以\foreach不同方式使用的循环。

啊啊我忘了\foreach使用组。这是本网站的一个众所周知的问题。好的,让我们继续使用如此流行的\foreach

% Command for printing skills text
\newtoks\skillstexttoks
\newcommand\skillstext[1]{%
    \renewcommand{\skillstext}{%
    \global\skillstexttoks{\begin{tabular}{cc}}%
       \foreach [count=\i] \x in {#1}{%
       \ifnumodd\i{\global\skillstexttoks\expandafter\expandafter\expandafter
                    {\expandafter\the\expandafter\skillstexttoks\x \\}}
                  {\global\skillstexttoks\expandafter\expandafter\expandafter
                    {\expandafter\the\expandafter\skillstexttoks\x &}}%
            }%
    \the\skillstexttoks
    \end{tabular}%
     }%
 }

现在我们得到了

在此处输入图片描述

我只能重申我的同事对\skillstext重新定义自身的方式的怀疑。无论如何,这就是我得到的结果。

相关内容