我正在嘗試自動生成一個表,該表計算其條目並為每個條目提供自訂標籤。我無法讓 \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}