我一直在阅读 LaTeX 文档,但我似乎无法弄清楚如何创建一个新的命令,该命令基本上是一个包含不同文本的部分(即,如果我调用\testsection
它会产生一个测试部分n, 在哪里n是测试部分编号。
我还应如何指定要使用哪个数字(例如 1/2/3 与 1.1/1.2/1.3 与 1.1.1/1.1.2/1.1.3 等)?
我找到了以下解决方法,但这会改变\subsection
,我想避免这种情况。出于某种原因,它也不会自动编号(它停留在1
)。
\renewcommand{\thesubsection}{Problem \arabic{section}}
\newcommand{\problem}{\subsection{}}
答案1
在 LaTeX 中,你可以使用命令定义计数器\newcounter
。例如,
\newcounter{test}
定义一个名为 的计数器test
。如果您希望此计数器在另一个计数器“内部”计数,以便每次该计数器更改时自动重置,则可以添加“父”计数器的名称作为可选参数。例如,要使测试计数器“内部”于部分计数器,您可以编写:
\newcounter{test}[section]
计数器的值test
使用 打印\thetest
。我们可以使用以下方法更改其格式:
\renewcommand\thetest{\thesection.\alph{test}}
下面的代码将所有这些包装到一个名为 的新部分中,类似于宏\testing
,它产生:
完整代码如下:
\documentclass{article}
\newcounter{test}[section]
\renewcommand\thetest{\thesection.\alph{test}}
\newcommand\testing[1]{%
\refstepcounter{test}%
\textbf{\thetest~#1}
}
\begin{document}
\section{First section}
\testing{one test}
\testing{two test}
\testing{three test}
\section{First section}
\testing{one test}
\testing{two test}
\testing{three test}
\end{document}
答案2
也许一个类似定理的环境testsection
可以testsection
帮到你,只要分段命令的计数器一踏,它的计数器就会重置?
\documentclass{article}
\usepackage{hyperref}
\newtheorem{testsection}{Test Section}%
\makeatletter
%--------------------------------------
\@addtoreset{testsection}{section}
\@addtoreset{testsection}{subsection}
\@addtoreset{testsection}{subsubsection}
\@addtoreset{testsection}{paragraph}
\@addtoreset{testsection}{subparagraph}
%--------------------------------------
\newcommand\CntValNotZero[4]{%
\ifnum\value{#3}<1 \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{#4}{\csname the#1#3\endcsname#2}%
}%
\renewcommand\thetestsection{%
\CntValNotZero{}{.}{subparagraph}{%
\CntValNotZero{}{.}{paragraph}{%
\CntValNotZero{}{.}{subsubsection}{%
\CntValNotZero{}{.}{subsection}{%
\CntValNotZero{}{.}{section}{}%
}%
}%
}%
}%
\arabic{testsection}%
}%
\@ifpackageloaded{hyperref}{%
\renewcommand\theHtestsection{%
\CntValNotZero{H}{.}{subparagraph}{%
\CntValNotZero{H}{.}{paragraph}{%
\CntValNotZero{H}{.}{subsubsection}{%
\CntValNotZero{H}{.}{subsection}{%
\CntValNotZero{H}{.}{section}{}%
}%
}%
}%
}%
\arabic{testsection}%
}%
}{}%
\makeatother
\pagestyle{empty}
\enlargethispage{1in}
\begin{document}
\vspace*{-1in}%
\section{First Section}
\begin{testsection}\label{testsectionlabel1.1}%
A test section inside First Section.
\end{testsection}
\begin{testsection}\label{testsectionlabel1.2}%
Another test section inside First Section.
\end{testsection}
\subsection{First Section's First Subsection}
\begin{testsection}\label{testsectionlabel1.1.1}%
A test section inside First Section's First Subsection.
\end{testsection}
\begin{testsection}\label{testsectionlabel1.1.2}%
Another test section inside First Section's First Subsection
\end{testsection}
\subsection{First Section's Second Subsection}
\begin{testsection}\label{testsectionlabel1.2.1}%
A test section inside First Section's Second Subsection.
\end{testsection}
\begin{testsection}\label{testsectionlabel1.2.2}%
Another test section inside First Section's Second Subsection
\end{testsection}
\section{Second Section}
\begin{testsection}\label{testsectionlabel2.1}%
A test section inside Second Section.
\end{testsection}
\begin{testsection}\label{testsectionlabel2.2}%
Another test section inside Second Section.
\end{testsection}
\subsection{Second Section's First Subsection}
\begin{testsection}\label{testsectionlabel2.1.1}%
A test section inside Second Section's First Subsection.
\end{testsection}
\begin{testsection}\label{testsectionlabel2.1.2}%
Another test section inside Second Section's First Subsection
\end{testsection}
\subsection{Second Section's Second Subsection}
\begin{testsection}\label{testsectionlabel2.2.1}%
A test section inside Second Section's Second Subsection.
\end{testsection}
\begin{testsection}\label{testsectionlabel2.2.2}%
Another test section inside Second Section's Second Subsection
\end{testsection}
Referencing:
\verb|\ref{testsectionlabel1.1}|: \ref{testsectionlabel1.1}
\verb|\ref{testsectionlabel1.2}|: \ref{testsectionlabel1.2}
\verb|\ref{testsectionlabel1.1.1}|: \ref{testsectionlabel1.1.1}
\verb|\ref{testsectionlabel1.1.2}|: \ref{testsectionlabel1.1.2}
\verb|\ref{testsectionlabel1.2.1}|: \ref{testsectionlabel1.2.1}
\verb|\ref{testsectionlabel1.2.2}|: \ref{testsectionlabel1.2.2}
\verb|\ref{testsectionlabel2.1}|: \ref{testsectionlabel2.1}
\verb|\ref{testsectionlabel2.2}|: \ref{testsectionlabel2.2}
\verb|\ref{testsectionlabel2.1.1}|: \ref{testsectionlabel2.1.1}
\verb|\ref{testsectionlabel2.1.2}|: \ref{testsectionlabel2.1.2}
\verb|\ref{testsectionlabel2.2.1}|: \ref{testsectionlabel2.2.1}
\verb|\ref{testsectionlabel2.2.2}|: \ref{testsectionlabel2.2.2}
\end{document}