
следующий пример показывает, что самоопределяемый счетчик в tabularx
среде не начинается с "1". Не могу понять, как определить команду, чтобы она всегда начиналась с "1". Идеи?
\documentclass{article}
\usepackage{enumitem}
\usepackage{cleveref}
\usepackage{etoolbox}
\usepackage{booktabs}
\usepackage{ltxtable}
\usepackage{ltablex}
\makeatletter\newcommand*{%
\CreateCounterIfNotExists}[1]{\begingroup%
\@ifundefined{c@#1}{\newcounter{#1}}{}%
\endgroup}\makeatother
\def\sdef#1{\expandafter\def\csname#1\endcsname}
\newcommand\requirement[3]{%
\CreateCounterIfNotExists{my#1}%
\sdef{themy#1}{#1\arabic{my#1}}%
\refstepcounter{my#1}%
\label{#2} #1\arabic{my#1}: #3%
}
\begin{document}
\requirement{A}{req:foo:a}{Starts with 1}
\requirement{A}{req:foo:b}{continues with 2}
\requirement{B}{req:hop:a}{Starts with 1}
\requirement{B}{req:hop:b}{continues with 2}
\begin{tabularx}{\textwidth}{lX}
\caption{Requirements}\\
\label{tbl:reqs}\\
\toprule
\textbf{Area}& \textbf{Description} \\\midrule
\requirement{C}{req:bar:A}{Starts with 3}
\requirement{C}{req:bar:B}{Continues with 4}
&
This should start with C1 \\\midrule
\end{tabularx}
\end{document}
решение1
Вы не можете определить счетчики, пока tabularx
выполняет свою работу.
Решением может быть определение нужного вам параметра заранее tabularx
или сделать это отложенно с помощью .aux
файла.
\documentclass{article}
\usepackage{enumitem}
\usepackage{etoolbox}
\usepackage{booktabs}
\usepackage{ltxtable}
\usepackage{ltablex}
\usepackage{cleveref}
\makeatletter
\newcommand\requirement[3]{%
\protected@write\@auxout{}{\string\CreateCounter{#1}}%
\@ifundefined{c@my#1}
{#1??: #3}
{\refstepcounter{my#1}\label{#2}#1\arabic{my#1}: #3}%
}
\newcommand{\CreateCounter}[1]{%
\@ifundefined{c@my#1}
{\newcounter{my#1}\global\@namedef{themy#1}{#1\arabic{my#1}}}
{}%
}
\AtEndDocument{\let\CreateCounter\@gobble}
\makeatother
\begin{document}
{\requirement{A}{req:foo:a}{Starts with 1}}
\requirement{A}{req:foo:b}{continues with 2}
\requirement{B}{req:hop:a}{Starts with 1}
\requirement{B}{req:hop:b}{continues with 2}
\noindent
\begin{tabularx}{\textwidth}{lX}
\caption{Requirements}\label{tbl:reqs}\\
\toprule
\textbf{Area}& \textbf{Description} \\
\midrule
\requirement{C}{req:bar:A}{Starts with 1}
\requirement{C}{req:bar:B}{Continues with 2} & This should start with C1 \\
\midrule
\end{tabularx}
\end{document}
При первом \requirement{X}
использовании число не будет сгенерировано, но оно будет сгенерировано при следующем запуске LaTeX.
решение2
Почти никогда не следует объявлять счетчик внутри макроса, его следует объявить один раз в преамбуле и просто использовать. Тогда он tabularx
будет знать о нем и сохранит его значение при выполнении настроек ширины пробной версии.
\documentclass{article}
\usepackage{enumitem}
\usepackage{cleveref}
\usepackage{etoolbox}
\usepackage{booktabs}
\usepackage{ltxtable}
\usepackage{ltablex}
\newcounter{myA}\renewcommand\themyA{A\arabic{myA}}
\newcounter{myB}\renewcommand\themyB{B\arabic{myB}}
\newcounter{myC}\renewcommand\themyC{C\arabic{myC}}
\newcommand\requirement[3]{%
\refstepcounter{my#1}%
\label{#2}#1\arabic{my#1}: #3%
}
\begin{document}
\requirement{A}{req:foo:a}{Starts with 1}
\requirement{A}{req:foo:b}{continues with 2}
\requirement{B}{req:hop:a}{Starts with 1}
\requirement{B}{req:hop:b}{continues with 2}
\begin{tabularx}{\textwidth}{lX}
\caption{Requirements}\\
\label{tbl:reqs}\\
\toprule
\textbf{Area}& \textbf{Description} \\\midrule
\requirement{C}{req:bar:A}{Starts with 3}
\requirement{C}{req:bar:B}{Continues with 4}
&
This should start with C1 \\\midrule
\end{tabularx}
\end{document}
решение3
Как упоминалось в решениях Дэвида Карлайла и egreg: счетчики не должны быть определены on-the-fly
.
Я использовал тот же подход, что и Дэвид Карлайл, предоставив команды \NewDocumentCounter
, \DeclareRequirement
, DeclareRequirements
которые можно использовать только в преамбуле, чтобы предотвратить случайное использование.
\documentclass{article}
\usepackage{enumitem}
\usepackage{etoolbox}
\usepackage{booktabs}
\usepackage{ltxtable}
\usepackage{ltablex}
\usepackage{cleveref}
\usepackage{xparse}
\NewDocumentCommand{\NewDocumentCounter}{m}{%
\ifltxcounter{#1}{%
}{%
\typeout{Creating counter #1}
\newcounter{#1}%
}
}
\NewDocumentCommand{\DeclareRequirement}{m}{%
\NewDocumentCounter{my#1}%
\expandafter\renewcommand\csname themy#1\endcsname{#1\arabic{my#1}}
}%
\listgadd\reqlist{}
\NewDocumentCommand{\DeclareRequirements}{+m}{%
\forcsvlist{\listgadd\reqlist}{#1}
\forlistloop{\DeclareRequirement}{\reqlist}
}%
\makeatletter
\@onlypreamble{\NewDocumentCounter}
\@onlypreamble{\DeclareRequirement}
\@onlypreamble{\DeclareRequirements}
\makeatother
\DeclareRequirements{A,B,C} % Declare all requirement counters in a row ...
\newcommand\requirement[3]{%
\refstepcounter{my#1}%
\label{#2} #1\arabic{my#1}: #3%
}
\begin{document}
\requirement{A}{req:foo:a}{Starts with 1}
\requirement{A}{req:foo:b}{continues with 2}
\requirement{B}{req:hop:a}{Starts with 1}
\requirement{B}{req:hop:b}{continues with 2}
\begin{tabularx}{\textwidth}{lX}
\caption{Requirements}\\
\label{tbl:reqs}\\
\toprule
\textbf{Area}& \textbf{Description} \\\midrule
\requirement{C}{req:bar:A}{Starts with 3 no longer} %\themyC
\requirement{C}{req:bar:B}{Continues with 4 no longer} %\themyC
&
This should start with C1 \\\midrule
\end{tabularx}
\end{document}