双小节标题

双小节标题

我正在编写一份“报告”,其中包含几个作业的解决方案,这些作业分为“主题”和“部分”。
我对仅使用部分(标题等于主题名称)得到的结果非常满意,我用重新定义了子部分命令\renewcommand{\thesubsection}{Part \arabic{subsection}}。这很好用。
但现在我想将两部分的答案合并为一个。是否可以将一个小节作为“第 2 和 3 部分“?

\documentclass[11pt,a4paper]{article}
\renewcommand{\thesubsection}{Part \arabic{subsection}}

\begin{document}
\section{Polytropes}
The code used for the computations in this section can be seen in Appendix 1.
\subsection{}
\textbf{ASSIGNMENT}\\
my solution.
\subsection{and 3} %this obviously horrible due to spacing, referencing, counter, etc. 
\textbf{ASSIGNMENT of part 2 and 3}\\
my solution.
\end{document}

答案1

我会避免重载\subsection并在此基础上定义适当的命令。在代码中,我没有使用编号部分,而是使用\subsection*并传递预期的标题设置\@currentlabel,它负责正确的交叉引用。

\documentclass[11pt,a4paper]{article}

\makeatletter
\newcommand{\documentpart}{%
  \stepcounter{subsection}%
  \def\@currentlabel{Part \arabic{subsection}}%
  \subsection*{\@currentlabel}%
  \addcontentsline{toc}{subsection}{\@currentlabel}%
}
\newcommand{\extradocumentpart}{%
  \stepcounter{subsection}%
  \def\@currentlabel{Part \@arabic{\c@subsection} and \@arabic{\numexpr\c@subsection+1\relax}}%
  \subsection*{\@currentlabel}
  \addcontentsline{toc}{subsection}{\@currentlabel}%
  \stepcounter{subsection}%
}
\makeatother

\begin{document}

\tableofcontents

\section{Polytropes}

The code used for the computations in this section can be seen in Appendix~1.

\documentpart\label{test1}
\textbf{ASSIGNMENT of \ref{test1}}\\
my solution.

\extradocumentpart\label{test2}
\textbf{ASSIGNMENT of \ref{test2}}\\
my solution.

\documentpart\label{test3}
\textbf{ASSIGNMENT of \ref{test3}}\\
my solution.

\end{document}

在此处输入图片描述

如果你也想要实际的标题

\documentclass[11pt,a4paper]{article}

\makeatletter
\newcommand{\documentpart}[1]{%
  \stepcounter{subsection}%
  \def\@currentlabel{Part \arabic{subsection}}%
  \subsection*{\@currentlabel\quad#1}%
  \addcontentsline{toc}{subsection}{\@currentlabel\quad#1}%
}
\newcommand{\extradocumentpart}[1]{%
  \stepcounter{subsection}%
  \def\@currentlabel{Part \@arabic{\c@subsection} and \@arabic{\numexpr\c@subsection+1\relax}}%
  \subsection*{\@currentlabel\quad#1}
  \addcontentsline{toc}{subsection}{\@currentlabel\quad#1}%
  \stepcounter{subsection}%
}
\makeatother

\begin{document}

\tableofcontents

\section{Polytropes}

The code used for the computations in this section can be seen in Appendix~1.

\documentpart{Some title}\label{test1}
\textbf{ASSIGNMENT of \ref{test1}}\\
my solution.

\extradocumentpart{Some title}\label{test2}
\textbf{ASSIGNMENT of \ref{test2}}\\
my solution.

\documentpart{Some title}\label{test3}
\textbf{ASSIGNMENT of \ref{test3}}\\
my solution.

\end{document}

在此处输入图片描述

相关内容