更新

更新

我按行创建一个表,如下所示:

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

我如何证明,如果第一个参数是 0 或 1,并且如果是 1,则设置行颜色?

我知道,我必须使用该table xcolor包,但是对于 if 条件,如果可能的话,我不想使用任何包。

多谢

史努比

答案1

这并不能回答您的问题,但可以解决您的需求(在您的 XY 问题中,回答 X :-)

在问题生成的表格中“不使用 CSV 如何处理具有许多参数的表记录?",您需要根据星期几为表格中的某些行指定不同的颜色。您不需要引入额外的参数来表示星期几,因为您已经在其他参数中有了星期几的名称。

最简单的解决方案是根据星期几的名称定义颜色:

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

然后\rowcolor在生成表的代码中引入命令:

笔记:此实现存在错误。请参阅此答案末尾的更新。

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

测试:

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

结果:

结果

完整代码如下,供参考:

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

更新

正如 OP 在评论中指出的那样,上述实现会产生Misplaced \noalign错误。这是因为\rowcolor{}命令应该是表格行中第一个使用的命令,而我的实现\ExtractData首先使用了其他宏()来处理参数。

我不确定为什么使用\ExtractData会干扰表行机制,因为\ExtractData不应该产生任何输出或跳过,所以我不知道如何正确修复该问题。不过我有两个解决方法。

  1. 为 提供一个额外的参数\row,就像 OP 最初提出的问题一样。例如,第一个参数表示行01“种类”,用于选择颜色。这样,可以在任何之前用作实现\rowcolor{}中的第一件事\row\ExtractData

  2. 有点儿像 hack。使用我原来的解决方案,但\\在 之前插入\rowcolor{}。这会创建一个新的表格行,并且\rowcolor将成为该行中的第一个内容。当然,问题是每个\row命令实际上都会产生两个表格行,一个是空的,另一个有内容。如果\\我们使用而不是 ,则可以使空行不可见\\[-12pt](因为在这种情况下 12pt 是空行的高度)。

下面的例子实现了这两种方法。第一种方法称为\rowA,第二种称为\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}

结果:

结果

答案2

与你的提议类似,使用基本的 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}

答案3

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

请注意,\else大小写是可选的。

相关内容