1

I'm working on a "report" which consists of the solutions for several assignments which are structured into "topics" and "parts".
I'm quite happy with the result I got by simply using sections (with title equal to topic name) and I redefined the subsection command with \renewcommand{\thesubsection}{Part \arabic{subsection}}. Which works fine.
But right now I would like to merge the answer of two parts into one. Is it possible to start one subsection as "Part 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
  • Welcome to TeX-SX! As a new member, it is recommended to visit the Welcome and the Tour pages to be informed about our format and also to know about Minimal Example. Otherwise, more specifically about your question: a Minimal Example would help to illustrate and clarify your question.
    – R. N
    Dec 22, 2020 at 14:19

1 Answer 1

2

I'd avoid overloading \subsection and define a proper command based on it. In the code, instead of a numbered section, I use \subsection* and pass the expected title, setting \@currentlabel, which is responsible for the correct cross reference.

\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}

enter image description here

If you also want actual titles

\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}

enter image description here

8
  • 1
    Thank you very much! This definitely works! There is a small peculiarity though: in the toc, the "dots" seem to ignore the numbers (in this case "2 and 3" has the dots just underneath it)
    – ahermann
    Dec 22, 2020 at 21:59
  • @Andrej Revamped code, now there is no problem with the toc.
    – egreg
    Dec 22, 2020 at 22:35
  • Thank you very much!!! (edit because I typed my answer while you already enhanced the code) Is it possible to add the possibility of a title to the \documentpart command?
    – ahermann
    Dec 22, 2020 at 22:36
  • changing it to \newcommand{\documentpart}[1]{% \stepcounter{subsection}% \def\@currentlabel{Part \arabic{subsection}\hspace{1em}#1} allows to add a title, but then the referencing gets messy as it also includes the title into the reference...
    – ahermann
    Dec 22, 2020 at 23:00
  • @Andrej I can't read your mind, sorry. Did you speak about titles?
    – egreg
    Dec 22, 2020 at 23:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .