0

I create a table by rows like this:

\row{1}{Sunday,Su,05.09.2015}{N}{08:00,17:00,12:00,13:00}{8.0,1}{Airplane Hangar}

How can I prove, if the first parameter is 0 or 1, an if it's 1 then set a rowcolor?

I know, that I have to use the table xcolorpackage, but for the if contidion, I don't want to use any package, if it's possible.

Thanks a lot

Snoopy.EL

JLDiaz
  • 55,732
SamHoff
  • 89
  • I showed an answer for low-level conditionals. I'm not sure what do you mean by "set rowcolor". If you show a minimal working example, someone may provide a full solution. – yo' Oct 08 '15 at 10:15
  • Sorry, I thought it would be possible only with this single line. So, what I want to do is to format specific lines from this: http://tex.stackexchange.com/questions/271380/how-can-i-handle-table-record-with-many-parameters-without-csv

    I have many rows and i want to set the saturdays and sundays (thats the first value '{1}' to an rowcolor like gray for example.

    I hope, it's now more comprehensible.

    – SamHoff Oct 14 '15 at 10:06
  • 2
    I answered in the best possible way I could. I won't answer a question that is scattered through two question text and a long comment. Maybe someone else will. – yo' Oct 14 '15 at 10:11
  • @SamHoff: Your question is pretty unclear and as yo' effectively wrote: Glueing code from multiple questions and answers is not our 'job'. A single code line with \row.... etc. without any further information on where and what should be coloured isn't sufficient –  Oct 14 '15 at 11:14

3 Answers3

3

This does not answer your question, but solves your need (answer to X, in your XY problem :-)

In the table generated in the question "How can I handle table record with many parameters without CSV?", you need to have some rows of the table in different color based on the day of the week. You don't need to introduce an extra parameter to denote the day of the week, since you already have the name of the day in other parameters.

The easiest solution is to define colors based on weekday names:

\colorlet{Montag}{white}
\colorlet{Dienstag}{white}
\colorlet{Mittwoch}{white}
\colorlet{Donnerstag}{white}
\colorlet{Freitag}{white}
\colorlet{Samstag}{red!30}
\colorlet{Sonntag}{red!30}

And then introduce the \rowcolor command in the code that generates your table:

Note: This implementation had a bug. See UPDATE at the end of this answer.

\def\row#1#2#3#4{%
\ExtractData{#1}{\formday,\formdayshort,\formdate}%
\ExtractData{#2}{\formcome,\formgo,\formbreak,\formbreakgo}%
\ExtractData{#3}{\formhours,\formdays}%
\ExtractData{#4}{\formproject}%
\rowcolor{\formday}          % <------ HERE!
\FormDayShort{\formdayshort} & \FormDate{\formdate}       & \FormType{U} &
 \FormCome{\formcome}         & \FormGo{\formgo}           &
%\formBreak{\formbreak}       & \FormBreakGo{\formbreakgo} &
 \FormHours{\formhours}       & \FormDays{\formdays}       & \FormProject{\formproject}
 \tabularnewline
}

Testing:

{\huge Timesheet} \hfill

\begin{timesheet}
\row{Dienstag,Di,01.09.2015}{08:00,17:00,12:00,13:00}{{08,00},1}{Airplane Hangar}
\row{Mittwoch,Mi,02.09.2015}{08:30,17:30,12:00,13:00}{{08,00},1}{Spaceship Hangar}
\row{Donnerstag,Do,03.09.2015}{08:30,17:00,12:00,13:00}{{07,50},1}{Autocar Hangar}
\row{Freitag,Fr,04.09.2015}{08:15,17:15,12:00,13:00}{{08,00},1}{Airplane Hangar}
\row{Samstag,Mi,02.09.2015}{08:30,17:30,12:00,13:00}{{08,00},1}{Spaceship Hangar}
\row{Sonntag,So,04.09.2015}{08:15,17:15,12:00,13:00}{{08,00},1}{Airplane Hangar}
\end{timesheet}

Result:

Result

Here is the complete code, for reference:

\documentclass[10pt,a4paper]{article}
\usepackage[ngerman]{babel} 
\usepackage{longtable}
\usepackage{pgffor}
\usepackage[table]{xcolor}

\colorlet{Montag}{white}
\colorlet{Dienstag}{white}
\colorlet{Mittwoch}{white}
\colorlet{Donnerstag}{white}
\colorlet{Freitag}{white}
\colorlet{Samstag}{red!30}
\colorlet{Sonntag}{red!30}

\def\ExtractData#1#2{% #1 List of values, #2 list of macros
\def\ccc{i}%
\foreach \data in {#1} {% We create auxilar macros called \auxi, \auxii, \auxiii...
  \expandafter\xdef\csname aux\ccc\endcsname{\data}%
  \xdef\ccc{\ccc i}% 
  }%
\xdef\ccc{i}%
\foreach \macro in {#2} {% And then store them into the provided macros
  \expandafter\xdef\macro{\csname aux\ccc\endcsname}%
  \xdef\ccc{\ccc i}%
  }%
}

\def\row#1#2#3#4{%
\ExtractData{#1}{\formday,\formdayshort,\formdate}%
\ExtractData{#2}{\formcome,\formgo,\formbreak,\formbreakgo}%
\ExtractData{#3}{\formhours,\formdays}%
\ExtractData{#4}{\formproject}%
\rowcolor{\formday}%
 \FormDayShort{\formdayshort} & \FormDate{\formdate}       & \FormType{U} &
 \FormCome{\formcome}         & \FormGo{\formgo}           &
%\formBreak{\formbreak}       & \FormBreakGo{\formbreakgo} &
 \FormHours{\formhours}       & \FormDays{\formdays}       & \FormProject{\formproject}
 \tabularnewline
}

% Your original macros (I guess their purpose is to be able to change the formatting of the fields)
\newcommand*{\FormDay}[1]{#1}
\newcommand*{\FormDayShort}[1]{#1}
\newcommand*{\FormDate}[1]{#1}
\newcommand*{\FormType}[1]{#1}
\newcommand*{\FormCome}[1]{#1}
\newcommand*{\FormGo}[1]{#1}
\newcommand*{\FormBreak}[1]{#1}
\newcommand*{\FormBreakGo}[1]{#1}
\newcommand*{\FormHours}[1]{#1}
\newcommand*{\FormDays}[1]{#1}
\newcommand*{\FormProject}[1]{#1}

% Your original environment (whith the sample row deleted)
\newenvironment{timesheet}
{\begin{longtable} 
{@{}p{0.075\textwidth}p{0.125\textwidth}p{0.075\textwidth}p{0.1\textwidth}p{0.1\textwidth}p{0.075\textwidth}p{0.075\textwidth}p{0.25\textwidth}}     
Day   & Date    & Type  & Come   & Go   & Hours   & Days    & Project   \tabularnewline[2ex]
\endhead
}
{
\hline 
\end{longtable}
}

% Example of usage
\begin{document}
{\huge Timesheet} \hfill

\begin{timesheet}
\row{Dienstag,Di,01.09.2015}{08:00,17:00,12:00,13:00}{{08,00},1}{Airplane Hangar}
\row{Mittwoch,Mi,02.09.2015}{08:30,17:30,12:00,13:00}{{08,00},1}{Spaceship Hangar}
\row{Donnerstag,Do,03.09.2015}{08:30,17:00,12:00,13:00}{{07,50},1}{Autocar Hangar}
\row{Freitag,Fr,04.09.2015}{08:15,17:15,12:00,13:00}{{08,00},1}{Airplane Hangar}
\row{Samstag,Mi,02.09.2015}{08:30,17:30,12:00,13:00}{{08,00},1}{Spaceship Hangar}
\row{Sonntag,So,04.09.2015}{08:15,17:15,12:00,13:00}{{08,00},1}{Airplane Hangar}
\end{timesheet}
\end{document}

Update

As noted by the OP in a comment, above implementation produces Misplaced \noalign errors. This is because \rowcolor{} command should be the first one used in a table row, and my implementation used other macros (\ExtractData) to process the arguments first.

I'm not sure why the use of \ExtractData interferes with the table row mechanism, since \ExtractData should not produce any output nor skips, so I don't know how to properly fix the problem. However I have two ideas for workarounds.

  1. Provide an extra parameter to \row, as in the question originally asked by the OP. For example, a first argument which denotes with 0 or 1 the "kind" of row, to select a color. This way, \rowcolor{} can be used as the first thing inside the \row implementation, before any \ExtractData

  2. Kind of a hack. Use my original solution, but insert \\ before \rowcolor{}. This creates a new table row, and \rowcolor will be the first thing in that row. The problem of course is that each \row command will produce actually two table rows, one empty and the other with the contents. The emtpy ones can be made invisible if instead of \\ we use \\[-12pt] (because 12pt is the height of an empty row in this case).

The following example implements both approaches. The first one is called \rowA and the second one \rowB.

\documentclass[10pt,a4paper]{article}
\usepackage[ngerman]{babel} 
\usepackage{longtable}
\usepackage{pgffor}
\usepackage[table]{xcolor}


\def\ExtractData#1#2{% #1 List of values, #2 list of macros
\def\ccc{i}%
\foreach \data in {#1} {% We create auxilar macros called \auxi, \auxii, \auxiii...
  \expandafter\xdef\csname aux\ccc\endcsname{\data}%
  \xdef\ccc{\ccc i}% 
  }%
\xdef\ccc{i}%
\foreach \macro in {#2} {% And then store them into the provided macros
  \expandafter\xdef\macro{\csname aux\ccc\endcsname}%
  \xdef\ccc{\ccc i}%
  }%
}

\def\rowA#1#2#3#4#5{%
\rowcolor{color#1}%
\ExtractData{#2}{\formday,\formdayshort,\formdate}%
\ExtractData{#3}{\formcome,\formgo,\formbreak,\formbreakgo}%
\ExtractData{#4}{\formhours,\formdays}%
\ExtractData{#5}{\formproject}%
\FormDayShort{\formdayshort} & \FormDate{\formdate}       & \FormType{U} &
 \FormCome{\formcome}         & \FormGo{\formgo}           &
%\formBreak{\formbreak}       & \FormBreakGo{\formbreakgo} &
 \FormHours{\formhours}       & \FormDays{\formdays}       & \FormProject{\formproject}
\\
}
\colorlet{color0}{white}
\colorlet{color1}{black!30}

\def\rowB#1#2#3#4{%
\ExtractData{#1}{\formday,\formdayshort,\formdate}%
\ExtractData{#2}{\formcome,\formgo,\formbreak,\formbreakgo}%
\ExtractData{#3}{\formhours,\formdays}%
\ExtractData{#4}{\formproject}%
\\[-12pt]\rowcolor{\formday}%
\FormDayShort{\formdayshort} & \FormDate{\formdate}       & \FormType{U} &
 \FormCome{\formcome}         & \FormGo{\formgo}           &
%\formBreak{\formbreak}       & \FormBreakGo{\formbreakgo} &
 \FormHours{\formhours}       & \FormDays{\formdays}       & \FormProject{\formproject}
\\
}
\colorlet{Montag}{white}
\colorlet{Dienstag}{white}
\colorlet{Mittwoch}{white}
\colorlet{Donnerstag}{white}
\colorlet{Freitag}{white}
\colorlet{Samstag}{red!30}
\colorlet{Sonntag}{red!30}



% Your original macros (I guess their purpose is to be able to change the formatting of the fields)
\newcommand*{\FormDay}[1]{#1}
\newcommand*{\FormDayShort}[1]{#1}
\newcommand*{\FormDate}[1]{#1}
\newcommand*{\FormType}[1]{#1}
\newcommand*{\FormCome}[1]{#1}
\newcommand*{\FormGo}[1]{#1}
\newcommand*{\FormBreak}[1]{#1}
\newcommand*{\FormBreakGo}[1]{#1}
\newcommand*{\FormHours}[1]{#1}
\newcommand*{\FormDays}[1]{#1}
\newcommand*{\FormProject}[1]{#1}

% Your original environment (whith the sample row deleted)
\newenvironment{timesheet}
{
\begin{longtable}
{p{0.075\textwidth}p{0.125\textwidth}p{0.075\textwidth}p{0.1\textwidth}p{0.1\textwidth}p{0.075\textwidth}p{0.075\textwidth}p{0.25\textwidth}}     
Day   & Date    & Type  & Come   & Go   & Hours   & Days    & Project   \tabularnewline[2ex]
\endhead
}
{
\hline 
\end{longtable}
}

% Example of usage
\begin{document}
{\huge Timesheet} \hfill

\begin{timesheet}
\rowA{0}{Dienstag,Di,01.09.2015}{08:00,17:00,12:00,13:00}{{08,00},1}{Airplane Hangar}
\rowA{0}{Mittwoch,Mi,02.09.2015}{08:30,17:30,12:00,13:00}{{08,00},1}{Spaceship Hangar}
\rowA{0}{Donnerstag,Do,03.09.2015}{08:30,17:00,12:00,13:00}{{07,50},1}{Autocar Hangar}
\rowA{0}{Freitag,Fr,04.09.2015}{08:15,17:15,12:00,13:00}{{08,00},1}{Airplane Hangar}
\rowA{1}{Samstag,Sa,02.09.2015}{08:30,17:30,12:00,13:00}{{08,00},1}{Spaceship Hangar}
\rowA{1}{Sonntag,So,04.09.2015}{08:15,17:15,12:00,13:00}{{08,00},1}{Airplane Hangar}
\end{timesheet}

\begin{timesheet}
\rowB{Dienstag,Di,01.09.2015}{08:00,17:00,12:00,13:00}{{08,00},1}{Airplane Hangar}
\rowB{Mittwoch,Mi,02.09.2015}{08:30,17:30,12:00,13:00}{{08,00},1}{Spaceship Hangar}
\rowB{Donnerstag,Do,03.09.2015}{08:30,17:00,12:00,13:00}{{07,50},1}{Autocar Hangar}
\rowB{Freitag,Fr,04.09.2015}{08:15,17:15,12:00,13:00}{{08,00},1}{Airplane Hangar}
\rowB{Samstag,Sa,02.09.2015}{08:30,17:30,12:00,13:00}{{08,00},1}{Spaceship Hangar}
\rowB{Sonntag,So,04.09.2015}{08:15,17:15,12:00,13:00}{{08,00},1}{Airplane Hangar}
\end{timesheet}
\end{document}

Result:

Result

JLDiaz
  • 55,732
  • Thank you @JLDiaz, it would be a good solution, but your example doesn't work.... :-( :-( – SamHoff Oct 15 '15 at 06:44
  • @samhoff what's the problem? It worked for me – JLDiaz Oct 15 '15 at 06:52
  • Get a misplaced \noalign error... :-/ – SamHoff Oct 15 '15 at 07:16
  • @SamHoff You are right. I was trying these examples in Sharelatex, which continues the compilation even if errors are present, and got the expected result, without looking at the log file. Those "misplaced \noalign" errors were there. I'll edit the answer to provide a solution. – JLDiaz Oct 15 '15 at 07:43
  • Thanks a lot, that's it! :) I think, I will use the '\rowA'-solution :) – SamHoff Oct 15 '15 at 09:55
2

Similar to yo''s proposition, with elementary TeX \ifnum#1 = 1 ...\else \fi

\documentclass{article}

\usepackage{xcolor}

\newcommand{\row}[6]{%
\ifnum#1 = 1
{\color{red}#2}
\else
{\color{blue}#2}
% do something other
\fi
}


\begin{document}

\row{0}{This is not red}{}{}{}{}

\row{1}{This is red}{}{}{}{}

\end{document}
  • Thank you @Christian Hupfer, it's almost the right way, but I want to set the complete rowcolor, and not the font. – SamHoff Oct 14 '15 at 09:51
  • @SamHoff: Yes, but you did not provide some help in your question ;-) –  Oct 14 '15 at 09:53
1
\newcommand\row[6]{%
  \ifcase#1\relax
    % code for case 0
  \or
    % code for case 1
  \or
    % code for case 2
  ...
  \else
    % code for other cases
  \fi
}

Note that \else case is optional.

yo'
  • 51,322