# Fill table automatically with a\ifthenelse inside a \foreach loop

Hi i've been trying to fill a 2 column table using a \foreach loop and inside i use a conditional to see if i do a line break or not:

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


however this wont work also i dont know if its important but in the error message it says its too big for \hbox, i ghuess its because it is in a sidebar that i created

UPDATE: example code Hi so this is what i'm trying to make work:

Here is the .tex code

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

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

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

\begin{document}

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

%----------------------------------------------------------------------------------------
%    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

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

\makeprofile % Print the sidebar

\end{document}


and here is the .cls code (where the skillstext command is):

\ProvidesClass{cvdocex}[2015/02/28 CV class]
\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

% 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{\cvdate}{}}{}{\textsc{\Large\icon{\Info}} & \cvdate\\}
\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}

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

\begin{justify}
\end{justify}

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

\profilesection{Skills}{4cm}

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

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

\end{textblock}
}


Sorry forgot to metion this is a modified version of the twenty second CV template class. Downloaded from here :http://www.latextemplates.com/template/twenty-seconds-resumecv all credits go to the author

• However defining a macro to redefine itself is never likely to come up smelling of roses .... – cfr Oct 10 '16 at 0:33
• So you only want to use the command once? That seems rather inflexible. What if you change your mind? I'd recommend doing this with a comma-separated list which you can map to processing code. If you only need to do this once, just store the list and process the arguments in the list when you \makeprofile. I don't see the benefit in processing them earlier and storing the result. But perhaps I am missing something. Note that your definitions contain lots of spurious (presumably spurious) spaces and paragraph breaks. – cfr Oct 10 '16 at 1:08
• Why is this dreadful Twenty-Seconds-CV popping up all over the place for the last few days? And where is the FREAKING COPYRIGHT NOTE? – Johannes_B Oct 10 '16 at 6:55
• Personally, i think it is disrespectful to grab something you don't own but are allowed to use and republish it under a new license without mentioning the real author. If i would be a lawyer, i probably would just say that this is against the law. Please add the original copyright and meta block. – Johannes_B Oct 11 '16 at 6:09
• i'm not republishing it under a new liscence, i'm mearly modifying the class to personalize my own cv. – RenéB Oct 11 '16 at 18:16

First of all your \value{\i} will raise an error, as \i does not expand to the name of a LaTeX counter. Secondly, don't use \ifthenelse as your class loads etoolbox.

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


But it does not work because \foreach can not be used inside a tabular this way. Indeed tracing the code shows you are trying to do (after my fix above):

\skillstext -> \begin {tabular}{cc} \foreach [count=\i ] \x in {{LaTeX},
\i {\x \\}{\x &}} \end {tabular}


This won't work. You need an expandable loop or a loop designed to work inside a tabular or to use \foreach differently.

Arggh I had forgotten that \foreach uses groups. That's a well-known issue on this site. OK, let's stick to using the so popular \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}%
}%
}


And now we get

I can only iterate my colleagues suspicions about the way \skillstext redefines itself. Anyway that's the output I got.

• Thank you it works very well! i just dont understand how the toks work? – RenéB Oct 10 '16 at 16:53
• It is a device which is used by LaTeX internally but has no user documentation for LaTeX users. You can read the relevant things in the TeXBook or in TeX by Topic (texdoc topic). It is not absolutely needed to use a toks, a macro could be employed (and using e-TeX \unexpanded and the like we can have possibly simpler looking code with less expandafter's)... – user4686 Oct 10 '16 at 17:06