我正在尝试自动生成一个表格,该表格会计算其条目数并为每个条目提供自定义标签。我无法让 \customlabel 正常工作,它似乎总是引用列表的最后一个条目。有人知道如何解决这个问题吗?
谢谢你!
CLS 文件:
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myCLS}[bla]
\LoadClass[12pt]{article}
% requirements
\newcounter{NameOfTheNewCounter}
\setcounter{NameOfTheNewCounter}{0}
\newcommand\themissionreqnumber{%
\ifnum\value{NameOfTheNewCounter}<100 0\fi
\protect\arabic{NameOfTheNewCounter}}
\newcommand{\missionrequiremententries}{}
\newcommand{\missionrequirement}[3]{%
\protected@xdef\missionrequiremententries{\missionrequiremententries \protect\addtocounter{NameOfTheNewCounter}{10}MIS-\protect\themissionreqnumber & #2 & #3 \protect\\}\protect\customlabel{#1}{MIS-\protect\themissionreqnumber}}
\RequirePackage[a4paper,%
top=3.28cm, bottom=3.10cm,%
left=2.0cm, right=1.95cm,%
headsep=0.2cm,headheight=50pt,heightrounded,%
footskip=1.3cm]{geometry}% Change the page dimensions and margins
\RequirePackage[table]{xcolor}% Colours
\definecolor{lightgrey}{RGB}{230,230,230}
\RequirePackage{tabularx,booktabs,colortbl}% Improved tables
和主 .tex 文件(使用 XeLaTex 编译)
\documentclass{myCLS}
\usepackage[hidelinks]{hyperref}
\makeatletter
\newcommand{\customlabel}[2]{%
\protected@write \@auxout {}{\string \newlabel {#1}{{#2}{\thepage}{#2}{#1}{}} }%
}
\makeatother
\newcommand{\missionrequirementstable}{%
\noindent
\begin{table}[h]
\caption{Requirements}\label{tab:req}
{\footnotesize
\begin{tabularx}{\columnwidth}{p{2cm}|X|p{2cm}}
\hline
\rowcolor{lightgrey}{\bfseries ID} & {\bfseries Statement} & {\bfseries Parent ID}\\
\hline
\missionrequiremententries
\hline
\end{tabularx}
}
\end{table}
}
\begin{document}
\missionrequirement{myFirstLabel}{My text.}{\ref{mySecondLabel}}
\missionrequirement{mySecondLabel}{My new text.}{\ref{myFirstLabel}}
\missionrequirementstable
\ref{myFirstLabel}
\end{document}
答案1
嘿,我解决了这个问题。它似乎在 if 子句中
错误代码:
\newcommand\themissionreqnumber{%
\ifnum\value{NameOfTheNewCounter}<100 0\fi
\protect\arabic{NameOfTheNewCounter}}
更正(实际上没有测试过,但这也是我第一次尝试新代码时出现的错误,请参阅下面的工作代码)
\newcommand\themissionreqnumber{%
{\ifnum\value{NameOfTheNewCounter}<100 0\fi}
\protect\arabic{NameOfTheNewCounter}}
但是我现在用完全不同的方式编码:
\newcounter{NameOfTheNewCounter}
\renewcommand{\theNameOfTheNewCounter}{MIS-{\ifnum\value{NameOfTheNewCounter}<100 0\fi}\arabic{misreqs}}
\newcommand{\NameOfTheNewCounterCnt}[1]{%
\addtocounter{NameOfTheNewCounter}{10}% Step counter
\theNameOfTheNewCounter% Print counter
\addtocounter{NameOfTheNewCounter}{-1}\refstepcounter{NameOfTheNewCounter}\label{#1}}% Mark with label
\newcommand{\missionrequiremententries}{}
\newcommand{\missionrequirement}[3]{%
\protected@xdef\missionrequiremententries{\missionrequiremententries \protect\misreqCnt{#1} & #2 & #3 \protect\\}}
{\footnotesize
\begin{longtable}{p{1.7cm}|p{11.9cm}|p{2cm}}
\caption{Mission Requirements}\label{tab:mission_req}\\
\hline
\rowcolor{lightgrey}{\bfseries Req. ID} & {\bfseries Statement} & {\bfseries Parent ID}\\
\hline
\endhead
\missionrequiremententries
\hline
\end{longtable}}}
\begin{document}
\missionrequirement{myFirstLabel}{My text.}{\ref{mySecondLabel}}
\missionrequirement{mySecondLabel}{My new text.}{\ref{myFirstLabel}}
\missionrequirementstable
\ref{myFirstLabel}
\end{document}