带有子计数器的自动计数器,用于表格行的引用

带有子计数器的自动计数器,用于表格行的引用

我想要这样的东西:在 tabularx 环境中,每行都有一个可引用的自动计数器和子计数器。我可以找到一个单一计数器的解决方案这里,但无法将其适配到 tabularx。所以我尝试使用两个计数器直接实现它。如何引用 MainNumber 计数器和 SubNumber,而不使用最后一行中的肮脏解决方案?

\documentclass{article}
\usepackage{array} 

\usepackage{tabularx}

\newcounter{MainNumber}
\newcounter{SubNumber}[MainNumber]

\begin{document}
\begin{table}
  \begin{tabularx}{\textwidth}{lX}
  \refstepcounter{MainNumber} \label{firstMain} \theMainNumber & This is first main. \\
  \refstepcounter{SubNumber} \label{1firstSub} \theMainNumber.\theSubNumber & This is first sub \\
  \refstepcounter{SubNumber} \label{1secondSub} \theMainNumber.\theSubNumber & This is second sub\\
  \refstepcounter{MainNumber} \label{secondMain} \theMainNumber & This is second main. \\   
  \refstepcounter{SubNumber} \label{2firstSub} \theMainNumber.\theSubNumber & This is first sub \\
  \refstepcounter{SubNumber} \label{2secondSub} \theMainNumber.\theSubNumber & This is second sub\\ 
\end{tabularx}
\end{table}

second main first sub:~\ref{2firstSub}

shoud be: 2.1

dirty: \ref{secondMain}.\ref{1firstSub}

\end{document}

答案1

像这样?我们的想法是重新定义\theSubNumber(其中包含计数器值)以获得您想要的格式:<main>.<sub>

因此,您只需输入\theSubNumber而不是\theMainNumber.\theSubNumber

enter image description here

\documentclass{article}
\usepackage{array} 

\usepackage{tabularx}

\newcounter{MainNumber}
\newcounter{SubNumber}[MainNumber]
\renewcommand{\theSubNumber}{\theMainNumber.\arabic{SubNumber}} % <-- added here
\begin{document}
\begin{table}
  \begin{tabularx}{\textwidth}{lX}
  \refstepcounter{MainNumber} \label{firstMain}  \theMainNumber & This is first main. \\
  \refstepcounter{SubNumber}  \label{1firstSub}  \theSubNumber  & This is first sub \\
  \refstepcounter{SubNumber}  \label{1secondSub} \theSubNumber  & This is second sub\\
  \refstepcounter{MainNumber} \label{secondMain} \theMainNumber & This is second main. \\   
  \refstepcounter{SubNumber}  \label{2firstSub}  \theSubNumber  & This is first sub \\
  \refstepcounter{SubNumber}  \label{2secondSub} \theSubNumber  & This is second sub\\ 
\end{tabularx}
\end{table}

second main first sub:~\ref{2firstSub}


\end{document}

答案2

enter image description here

另一种编号形式automatic是定义一个新的列类型,比如“b”,并让步进和显示由b类型本身完成。

代码通过布尔“变量”\ifstepmain等确定是否应该步进主计数器或子计数器。

由于代码无法知道何时发生新的主步骤(除非指定了更多条件),因此必须使用 来切换至主步进\prepnextmainstep

使用使tabularx问题稍微复杂化,因为tabularx尝试多次“排版”表格内容,每次\global都必须进行设置。

\documentclass{article}
\usepackage{array} 

\usepackage{tabularx}

\newcounter{MainNumber}
\newcounter{SubNumber}[MainNumber]

\counterwithin{SubNumber}{MainNumber}



\newif\ifstepmain
\newcolumntype{b}{>{\ifstepmain\refstepcounter{MainNumber}\theMainNumber\global\stepmainfalse\else\refstepcounter{SubNumber}\theSubNumber\fi}l}

\newcommand{\prepnextmainstep}{%
  \global\stepmaintrue%
}

\stepmaintrue
\begin{document}
\begin{table}
  \begin{tabularx}{\textwidth}{bX}
    \label{firstMain}   & This is first main. \tabularnewline
    \label{1firstSub}   & This is first sub \tabularnewline
    \label{1secondSub}  & This is second sub \prepnextmainstep\tabularnewline  
    \label{secondMain}  & This is second main.\tabularnewline   
    \label{2firstSub}   & This is first sub \tabularnewline
    \label{2secondSub}  & This is second sub \prepnextmainstep \tabularnewline 
    \label{thirddMain}  & This is third main.\tabularnewline   
    \label{3firstSub}   & This is first sub \tabularnewline
    \label{3secondSub}  & This is second sub \prepnextmainstep \tabularnewline 
\end{tabularx}
\end{table}

second main first sub:~\ref{2firstSub} and \ref{3secondSub} 

shoud be: 2.1 and 3.2
\end{document}

相关内容