这是一个我已经工作了一段时间的项目,我正在实现这个新功能,即单元格着色。
我的函数通过接收未知数量的“武器”数据并将它们组成一个表格来工作。它分为两个部分。首先,我收集所有武器并将每个参数存储到一个数组中。然后,当所有武器都列出后,将使用另一个名为 makesuperweapon 的方法。此方法检索存储在数组中的所有数据并对其进行处理,将数据放入格式化的表中。
现在我的需求让这件事变得棘手,我需要根据逻辑语句对每一行进行着色。如我的下图所示(我手动完成的示例),您可以看到为什么我不能简单地使用交替线来确定颜色。
现在开始讲代码。我尽力减少不必要的代码(这已从我的主要 cls 文件中删除),但可能有一些我包含的包对于这一部分来说并不需要。从 cls fie 开始,我建立了一些全局变量:数组、计数器和布尔值。
以下是\weapon{}{}{}{}{}
传入参数以存储到五个数组中的函数:\weaponname
、\range
、\shots
、\damage
和\type
。它还跟踪当前正在管理的武器数量(读作“我的索引”)。
\makesuperweapon
用于确定列表是否带有名称输入,并调用相应的辅助函数。在此示例中,我仅使用了命名列表并禁用了选择器,手动调用辅助函数来演示我当前尝试解决问题的两种方法。同样,忽略注释掉的\unnamedsuperweaponhelper
。
回到实际问题,我尝试了两次来解决这个问题,但都没有成功。两次尝试都是在进入表环境之前尝试编译表的行——我尝试在表中放置循环,但失败了。
第一个 (typeone) 是使用\renewcommand\mylines
按顺序构造每一行的 a 构建的。第二个类型通过递归地将每一行连接在一起(我认为)。两者的问题在于顺序或执行。如果在表环境之外进行评估,代码似乎会中断,因为\value{mycount} *must* be evaluated inside the for loop, as the index gets changed each iteration. Similarly the logic code containing
rowcolor{mycellcolor} must also be evaluated each loop step. On the other hand,
rowcolor{mycellcolor}` 似乎会中断。
这就是我的问题,我需要找到一种方法来停止rowcolor{mycellcolor}
使用方法类型一的 while 求值,或者确保\value{mycount}
类型二的求值和逻辑。在我的示例中,您可以看到类型一不适用于单元格着色,类型二将尝试\value{mycount}
在最后使用值 8 进行求值,这是一个空白索引,因此它会为每一行生成一个空行。有人知道如何解决这个问题吗?我一直在尝试学习扩展(我认为这与求值是一回事),但仍然很困惑。
进入 class.cls
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myclass}[2016/01/15]
\LoadClass[letterpaper, 8pt]{extbook}
\RequirePackage{moresize}
\RequirePackage[english]{babel}
\RequirePackage{geometry}
\geometry{hmargin={0.75in,0.75in},vmargin={0.75in,0.75in},marginparwidth={0.8in},marginparsep={0in}}
\RequirePackage[usenames,dvipsnames,table]{xcolor}
\RequirePackage{fontspec} % for xelatex
\RequirePackage{amsmath} %for math
\RequirePackage{amsfonts} %American math society fonts
\RequirePackage{gensymb} %Used for symbols such as $\degree$
\RequirePackage{tabularx} %more tables
\RequirePackage{multirow} %hell
\RequirePackage{colortbl} %table cell colors
\definecolor{mycellcolor}{rgb}{0.88,1,1}
\RequirePackage{array} %for advanced table commands
\RequirePackage{enumerate} % enumerate
\RequirePackage{enumitem} % fixes lists and such (more formatting commands)
\RequirePackage{csvsimple} % allows csv to table
\RequirePackage{readarray} % something to parse with
\RequirePackage{arrayjob} % array data structures
\RequirePackage{forloop} %allows forloops
\RequirePackage{printlen} %\uselengthunit{in} and \printlength{\value}
\RequirePackage{xparse} %not sure what this does
\RequirePackage{etoolbox} %some programming things, not explored yet
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%******************************************variables*****************************************************%
\newcommand{\@emptymacro}{}% Used to test against an empty macro
\newcommand{\emptymacro}[1]{\renewcommand\@emptymacro{#1}}
%*******************************************arrays*******************************************************%
\newarray{\weaponname}
\newarray{\range}
\newarray{\shots}
\newarray{\damage}
\newarray{\type}
%*******************************************counters*****************************************************%
\newcount{\weaponcount}\weaponcount 1\relax
\newcounter{mycount}% I dont know why this doesnt work as a \newcount, I think forloop needs a \newcounter
\newcommand{\mylines}{}
%*******************************************booleans*****************************************************%
\newif\iftocolor
\tocolortrue
%********************************************************************************************************%
\newcommand{\weapon}[5]{%calling function used to build up data into my arrays
\weaponname(\weaponcount)={#1}
\range(\weaponcount)={#2}
\shots(\weaponcount)={#3}
\damage(\weaponcount)={#4}
\type(\weaponcount)={#5}
%\identifier(\weaponcount)={#6}
\advance\weaponcount 1\relax
}
\newcommand{\makesuperweapon}{
\checkweaponname(1)
\ifemptydata
%\unnamedsuperweaponhelper %commented out for this question only
\else
%\namedsuperweaponhelper %commented out for this example only
\fi
}
%commented out for this example, I am using only named lists to keep consistency to reduce confusion
%\newcommand{\unnamedsuperweaponhelper}{%this formats the table to exclude names
% \renewcommand{\mylines}{
% \forloop{mycount}{1}{\value{mycount} < \weaponcount}{
% %\iftocolor\rowcolor{mycellcolor}\else\fi my logic on tocolor is not done, but the point remains
% \themycount \range(\value{mycount})&\shots(\value{mycount})&\damage(\value{mycount})&\type(\value{mycount})\\
% }
% }
%
% %Table settings for un-named entrys, D D D T
% \newcolumntype{D}{>{\hsize=0.233333\linewidth \centering\arraybackslash}X}%
% \newcolumntype{T}{>{\hsize=0.3\linewidth \raggedright\arraybackslash}X}%
%
% \setlength{\tabcolsep}{0pt}
% \renewcommand{\arraystretch}{1.2}
% %construct table using only provided entries
% \begin{tabularx}{0.47\textwidth}{ D D D T }
% \textbf{Range} & \textbf{Shots} & \textbf{Damage} & \textbf{Type} \\
% \hline%---------------------------------------------------------------
% \mylines
% \end{tabularx}
% \renewcommand\mylines{}
%}
\newcommand{\namedsuperweaponhelpertypeone}{%this formats the table to include names type 1 (loop construction)
\renewcommand{\mylines}{
\forloop{mycount}{1}{\value{mycount} < \weaponcount}{
%\iftocolor\rowcolor{mycellcolor}\else\fi my logic on tocolor is not done, but the point remains
\themycount \weaponname(\value{mycount})&\range(\value{mycount})&\shots(\value{mycount})&\damage(\value{mycount})&\type(\value{mycount})\\
}
}
%Table settings for named entrys, N C C C T
\newcolumntype{N}{>{\hsize=0.4\linewidth \raggedright\arraybackslash}X}%
\newcolumntype{C}{>{\hsize=0.15\linewidth \centering\arraybackslash}X}%
\newcolumntype{T}{>{\hsize=0.3\linewidth \raggedright\arraybackslash}X}%
\setlength{\tabcolsep}{0pt}
\renewcommand{\arraystretch}{1.2}
%construct table using only provided entries
\begin{tabularx}{0.47\textwidth}{ N C C C T }
& \textbf{Range} & \textbf{Shots} & \textbf{Dam} & \textbf{Type} \\
\hline%---------------------------------------------------------------
\mylines
\end{tabularx}
\renewcommand\mylines{}
\weaponcount 1\relax
}
\newcommand{\namedsuperweaponhelpertypetwo}{%this formats the table to include names type 2 (loop concatenation)
\forloop{mycount}{1}{\value{mycount} < \weaponcount}{
\def\halfline{\iftocolor\rowcolor{mycellcolor}\else\fi% %same thing
\themycount \weaponname(\value{mycount})&\range(\value{mycount})&\shots(\value{mycount})&\damage(\value{mycount})&\type(\value{mycount})\\}
\expandafter\renewcommand\expandafter\mylines\expandafter{\mylines\halfline}
}
%Table settings for named entrys, N C C C T
\newcolumntype{N}{>{\hsize=0.4\linewidth \raggedright\arraybackslash}X}%
\newcolumntype{C}{>{\hsize=0.15\linewidth \centering\arraybackslash}X}%
\newcolumntype{T}{>{\hsize=0.3\linewidth \raggedright\arraybackslash}X}%
\setlength{\tabcolsep}{0pt}
\renewcommand{\arraystretch}{1.2}
%construct table using only provided entries
\begin{tabularx}{0.47\textwidth}{ N C C C T }
& \textbf{Range} & \textbf{Shots} & \textbf{Dam} & \textbf{Type} \\
\hline%---------------------------------------------------------------
\mylines
\end{tabularx}
\renewcommand\mylines{}
\weaponcount 1\relax
}
\newcommand{\EXAMPLEnamedsuperweaponhelper}{%this produces the proper output manually
%Table settings for named entrys, N C C C T
\newcolumntype{N}{>{\hsize=0.4\linewidth \raggedright\arraybackslash}X}%
\newcolumntype{C}{>{\hsize=0.15\linewidth \centering\arraybackslash}X}%
\newcolumntype{T}{>{\hsize=0.3\linewidth \raggedright\arraybackslash}X}%
\setlength{\tabcolsep}{0pt}
\renewcommand{\arraystretch}{1.2}
%construct table using only provided entries
\begin{tabularx}{0.47\textwidth}{ N C C C T }
& \textbf{Range} & \textbf{Shots} & \textbf{Dam} & \textbf{Type} \\
\hline%---------------------------------------------------------------
\weaponname(1)&\range(1)&\shots(1)&\damage(1)&\type(1)\\
\rowcolor{mycellcolor}\weaponname(2)&\range(2)&\shots(2)&\damage(2)&\type(2)\\
\weaponname(3)&\range(3)&\shots(3)&\damage(3)&\type(3)\\
\rowcolor{mycellcolor}\weaponname(4)&\range(4)&\shots(4)&\damage(4)&\type(4)\\
\rowcolor{mycellcolor}\weaponname(5)&\range(5)&\shots(5)&\damage(5)&\type(5)\\
\rowcolor{mycellcolor}\weaponname(6)&\range(6)&\shots(6)&\damage(6)&\type(6)\\
\weaponname(7)&\range(7)&\shots(7)&\damage(7)&\type(7)\\
\end{tabularx}
\renewcommand\mylines{}
\weaponcount 1\relax
}
\newcommand{\clearvariables}{%clears used variables
\emptymacro{}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\clearvariables{}%LEAVE AT BOTTOM!
以及 main.tex 文件
\documentclass{myclass}
\begin{document}
Here is method one
\weapon{browning}{30}{5}{4}{Machine-gun}
\weapon{colt 1911}{10}{2}{4}{Pistol}
\weapon{M16}{20}{3}{4}{Rifle}
\weapon{Winchester}{}{}{}{}
\weapon{\rule{2em}{0in}solid slug}{16}{2}{5}{Shotgun}
\weapon{\rule{2em}{0in}buck shot}{14}{2}{6}{Shotgun}
\weapon{Desert Eagle}{12}{1}{5}{Pistol}
\makesuperweapon\namedsuperweaponhelpertypeone%normaly this would all be combined, I separated them for this example
Here is method two
\weapon{browning}{30}{5}{4}{Machine-gun}
\weapon{colt 1911}{10}{2}{4}{Pistol}
\weapon{M16}{20}{3}{4}{Rifle}
\weapon{Winchester}{}{}{}{}
\weapon{\rule{2em}{0in}solid slug}{16}{2}{5}{Shotgun}
\weapon{\rule{2em}{0in}buck shot}{14}{2}{6}{Shotgun}
\weapon{Desert Eagle}{12}{1}{5}{Pistol}
\makesuperweapon\namedsuperweaponhelpertypetwo%normaly this would all be combined, I separated them for this example
Here is my manual example
\EXAMPLEnamedsuperweaponhelper
\end{document}
答案1
请参阅MEW 指令保持一致。
这是一个expl3
基于的解决方案。要切换rowcolor
,应告知编译器新插入的内容是否是武器亚型。在这种情况下,它必须保留前一个rowcolor
。否则,它会切换它。编译器通过 获知子类型\weapon*
。
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{xparse}
\definecolor{mycellcolor}{rgb}{0.88,1,1}
\ExplSyntaxOn
% a weapon list
\seq_new:N \g_weapon_seq
\int_new:N \g_colorstate_int % 1 = white, !1 = mycellcolor
\int_set:Nn {\g_colorstate_int} {-1} % initially white
\tl_new:N \l_temp_tl
% add a new weapon
\NewDocumentCommand{\weapon}{smmmmm}
{% #1 = star (case of weapon subtype)
% #2 = weaponname, #3 = range, #4 = shots, #5 = damage, #6 = type
\bool_if:NTF #1
{
\int_set:Nn {\g_colorstate_int} {\g_colorstate_int}
}{
\int_set:Nn {\g_colorstate_int} {-1*\g_colorstate_int}
}
\int_compare:nTF {\g_colorstate_int=1}
{
\seq_put_right:Nn \g_weapon_seq {1#2}
}{
\seq_put_right:Nn \g_weapon_seq {0#2}
}
}
% build the table
\NewDocumentCommand{\makesuperweapon}{}
{
\make_super_weapon:n {}
}
\cs_new_protected:Nn \make_super_weapon:n
{
\tl_new:N \l_tablebody_tl
% the table preamble
\tl_set:Nn \l_tablebody_tl
{
\begin{tabular}{lcccc}
& \textbf{Range} & \textbf{Shots}& \textbf{Dam}& \textbf{Type}\\
\hline
}
% add rows
\seq_map_inline:Nn {\g_weapon_seq}
{
\tl_clear_new:N \l_temp_tl
\tl_set:Nn \l_temp_tl {##1}
\int_compare:nNnTF{\tl_head:N \l_temp_tl}{=}{1}
{
\tl_put_right:Nn \l_tablebody_tl
{
\tl_tail:N ##1 \\ \hline
}
}{
\tl_put_right:Nn \l_tablebody_tl
{
\rowcolor{mycellcolor} \tl_tail:N ##1 \\ \hline
}
}
}
% end of the table
\tl_put_right:Nn \l_tablebody_tl { \end{tabular} }
% print the table
\tl_use:N \l_tablebody_tl
}
\ExplSyntaxOff
\begin{document}
\weapon{browning}{30}{5}{4}{Machine-gun}
\weapon{colt 1911}{10}{2}{4}{Pistol}
\weapon{M16}{20}{3}{4}{Rifle}
\weapon{Winchester}{}{}{}{}
\weapon*{\rule{2em}{0in}solid slug}{16}{2}{5}{Shotgun}
\weapon*{\rule{2em}{0in}buck shot}{14}{2}{6}{Shotgun}
\weapon{Desert Eagle}{12}{1}{5}{Pistol}
\makesuperweapon
\end{document}
输出