我与很多人一起从事一个项目,其中很多人对 LaTeX 并不十分熟悉。因此,我必须定义一个命令 (\timeplan{}),它简化了表格单元格的着色(每列代表 2025、2026、2027 和 2028 年的一个年度季度。例如:\timeplan{25Q3, 27Q1} 应该为第 25Q3 列和第 27Q1 列的单元格着色。
我原本以为我的命令可以工作,因为它在普通段落环境中可以工作(见下文)。但不知何故,我编写的代码在表格环境中不起作用。
我收到很多错误:
- 不完整的 \ifnum;行后的所有文本都被忽略...
- 缺少} 插入。
- 额外\其他。
- 额外\fi。
- 额外的对齐标签已更改为 \cr。
- 额外\fi。
- 额外\其他。
- 额外\fi。
- 不完整的 \ifnum;行后的所有文本都被忽略...
\documentclass{scrartcl}
\RequirePackage{etoolbox}
\RequirePackage[table]{xcolor}
\RequirePackage{tikz}
\RequirePackage{ctable}
\usetikzlibrary{calc}
\RequirePackage{zref-savepos}
\RequirePackage{pgffor}
\RequirePackage{xifthen}
\RequirePackage{xparse}
\RequirePackage{verbatim}
\definecolor{tablecolor}{RGB}{219,229,241}
\newcommand{\coloredcell}{\cellcolor{tablecolor}}
\newcounter{quartalcount}
\setcounter{quartalcount}{0}
\newcommand{\helperfct}[2]{%
\forcsvlist{\listadd\subset}{#1}
\ifinlist{#2}{\subset}{%
\ifnum\value{quartalcount}<16
\coloredcell \stepcounter{quartalcount} &
\else
\coloredcell \stepcounter{quartalcount}
\fi
}{%
\ifnum\value{quartalcount}<16
\ifnum\value{quartalcount}=0 %Workaround with dummy as first entry
\stepcounter{quartalcount}
\else
\stepcounter{quartalcount} &
\fi
\else
\stepcounter{quartalcount}
\fi}
}
\newcommand{\timeplan}[1]{\setcounter{quartalcount}{0}
\forcsvlist{\listgadd\fulllist}{Dummy, 25Q1, 25Q2,25Q3, 25Q4,26Q1,26Q2,26Q3,26Q4,27Q1,27Q2,27Q3,27Q4,28Q1,28Q2,28Q3,28Q4} %Somehow the first entry will always be false (don't know why) therefore I did a workaround with a dummy.
\forlistloop{\helperfct{#1}}{\fulllist}
}
\begin{document}
\begin{tabular}{|*{16}{p{0.0625\textwidth-2\tabcolsep}|}}
\hline
\timeplan{25Q3, 27Q1}
\hline
\end{tabular}
\end{document}
如果我在表格环境之外使用 \textbackslash 和 &,它看起来就像我想要的那样。
\documentclass{scrartcl}
\RequirePackage{etoolbox}
\RequirePackage[table]{xcolor}
\RequirePackage{tikz}
\RequirePackage{ctable}
\usetikzlibrary{calc}
\RequirePackage{zref-savepos}
\RequirePackage{pgffor}
\RequirePackage{xifthen}
\RequirePackage{xparse}
\RequirePackage{verbatim}
\definecolor{tablecolor}{RGB}{219,229,241}
\newcommand{\coloredcell}{\cellcolor{tablecolor}}
\newcounter{quartalcount}
\setcounter{quartalcount}{0}
\newcommand{\helperfct}[2]{%
\forcsvlist{\listadd\subset}{#1}
\ifinlist{#2}{\subset}{%
\ifnum\value{quartalcount}<16
\textbackslash coloredcell \stepcounter{quartalcount} \&
\else
\textbackslash coloredcell \stepcounter{quartalcount}
\fi
}{%
\ifnum\value{quartalcount}<16
\ifnum\value{quartalcount}=0 %Workaround with dummy as first entry
\stepcounter{quartalcount}
\else
\stepcounter{quartalcount} \&
\fi
\else
\stepcounter{quartalcount}
\fi}
}
\newcommand{\timeplan}[1]{\setcounter{quartalcount}{0}
\forcsvlist{\listgadd\fulllist}{Dummy, 25Q1, 25Q2,25Q3, 25Q4,26Q1,26Q2,26Q3,26Q4,27Q1,27Q2,27Q3,27Q4,28Q1,28Q2,28Q3,28Q4} %Somehow the first entry will always be false (don't know why) therefore I did a workaround with a dummy.
\forlistloop{\helperfct{#1}}{\fulllist}
}
\begin{document}
What I want to generate by my Command \verb|\timeplan{25Q3, 27Q1}|\\ (here i used \verb|\textbackslash coloredcell| and \verb|\&| instead of \verb|\coloredcell| and \verb|&|): \par
\vspace{1ex}
\timeplan{25Q3, 27Q1}\par
\vspace{2ex}
How the Output should look like (Here i used \verb|&&\coloredcell&&&&&&\coloredcell&&&&&&&| instead of \verb|\timeplan{25Q3, 27Q1}|:\\
\begin{tabular}{|*{16}{p{0.0625\textwidth-2\tabcolsep}|}}
\hline
&&\coloredcell&&&&&&\coloredcell&&&&&&&\\
\hline
\end{tabular}
\end{document}
有人可以帮忙吗?:)
干杯
答案1
您需要将表格信息收集到标记列表中,然后在流程完成时一次性输出标记列表。要了解我的意思,请查看我的代码中的 实例\addtotoks
,这是我将信息添加到标记列表中的地方。以及\mytoks
,这是标记列表本身的名称。
已编辑以重置\fulllist
,否则将会累积。
\documentclass{scrartcl}
\RequirePackage{etoolbox}
\RequirePackage[table]{xcolor}
\RequirePackage{tikz}
\RequirePackage{ctable}
\usetikzlibrary{calc}
\RequirePackage{zref-savepos}
\RequirePackage{pgffor}
\RequirePackage{xifthen}
\RequirePackage{xparse}
\RequirePackage{verbatim}
\newtoks\mytoks
\newcommand\addtotoks[1]{\global\mytoks\expandafter{\the\mytoks#1}}
\definecolor{tablecolor}{RGB}{219,229,241}
\newcommand{\coloredcell}{\cellcolor{tablecolor}}
\newcounter{quartalcount}
\setcounter{quartalcount}{0}
\newcommand{\helperfct}[2]{%
\forcsvlist{\listadd\subset}{#1}
\ifinlist{#2}{\subset}{%
\ifnum\value{quartalcount}<16
\addtotoks{\coloredcell &}\stepcounter{quartalcount}
\else
\addtotoks{\coloredcell }\stepcounter{quartalcount}
\fi
}{%
\ifnum\value{quartalcount}<16
\ifnum\value{quartalcount}=0 %Workaround with dummy as first entry
\stepcounter{quartalcount}
\else
\stepcounter{quartalcount} \addtotoks{ &}
\fi
\else
\stepcounter{quartalcount}
\fi}
}
\newcommand{\timeplan}[1]{\mytoks{}\let\fulllist\empty
\setcounter{quartalcount}{0}
\forcsvlist{\listgadd\fulllist}{Dummy, 25Q1, 25Q2,25Q3, 25Q4,26Q1,26Q2,26Q3,26Q4,27Q1,27Q2,27Q3,27Q4,28Q1,28Q2,28Q3,28Q4} %Somehow the first entry will always be false (don't know why) therefore I did a workaround with a dummy.
\forlistloop{\helperfct{#1}}{\fulllist}
\the\mytoks}
\begin{document}
\begin{tabular}{|*{16}{p{0.0625\textwidth-2\tabcolsep}|}}
\hline
\timeplan{25Q3, 27Q1}\\
\hline
\end{tabular}
\bigskip
\begin{tabular}{|*{16}{p{0.0625\textwidth-2\tabcolsep}|}}
\hline
\timeplan{25Q4, 26Q2}\\
\hline
\end{tabular}
\end{document}