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