# Standard text and section cross referencing from a single label

In my document I need to have text under a number of sections which in one final section at the end, repeats some of the text with accompanying section references.

2 Analysis
2.1 Assessment 1 Conclusion
Some initial text here with figure etc. to not be repeated.
First example of repeated text

2.2 Assessment 2 Conclusion Some initial text here with figure etc. to not be repeated.
Second example of repeated text

3 Conclusions
Some initial text here to introduce the section to not be repeated.
First example of repeated text (Section 2.1)
Second example of repeated text (Section 2.2)

I am currently using the following code (from here) to repeat the text which reaches the desired functionality.

\documentclass[english, fontsize=11pt, DIV=12, twoside, numbers=noendperiod]{scrartcl}
% Section Spacing
\RedeclareSectionCommand[beforeskip=-1sp,afterskip=12pt]{section}
\RedeclareSectionCommand[beforeskip=-1sp,afterskip=12pt]{subsection}
\RedeclareSectionCommand[beforeskip=-1sp,afterskip=12pt]{subsubsection}
\RedeclareSectionCommand[afterskip=12pt]{paragraph}
\RedeclareSectionCommand[afterskip=12pt]{subparagraph}

\newcommand\secnumindent{2cm}
\RedeclareSectionCommands[indent=-\secnumindent]{section,subsection,subsubsection,paragraph,subparagraph}

\renewcommand\sectionformat{\parbox{\secnumindent}{\thesection\autodot}}
\renewcommand\subsectionformat{\parbox{\secnumindent}{\thesubsection\autodot}}
\renewcommand\subsubsectionformat{\parbox{\secnumindent}{\thesubsubsection\autodot}}
\renewcommand\paragraphformat{\parbox{\secnumindent}{\theparagraph\autodot}}
\renewcommand\subparagraphformat{\parbox{\secnumindent}{\thesubparagraph\autodot}}

\makeatletter
\newcommand*{\labelrec}[2]{%
\begingroup
\def\@currentlabel{#2}%
\label{#1}%
\endgroup
#2%
}
\makeatother

\begin{document}
\section{Analysis}
\subsection{Assessment 1 Conclusion}\label{SecRec1}
Some initial text here with figure etc. to not be repeated.
\labelrec{Rec1}{First example of repeated text}

\subsection{Assessment 2 Conclusion}\label{SecRec2}
Some initial text here with figure etc. to not be repeated.
\labelrec{Rec2}{Second example of repeated text}

\section{Conclusions}
Some initial text here to introduce the section to not be repeated.
\ref{Rec1} (Section \ref{SecRec1})
\ref{Rec2} (Section \ref{SecRec2})
\end{document}


However, is it possible to make \ref{} automatically enter the (Section \ref{SecRec#}) such that the below code would suffice?

\section{Conclusions}
\ref{Rec1}
\ref{Rec2}


Edit: Hopefully clarified question, added MWE, removed mistake where \Section was mentioned instead of \section

• Welcome to TeX.SX! Can you please expand the code snippet that you have posted to a full minimal working example. It is much easier to help you if we can start with some compilable code that illustrates your problem. A MWE should start with a \documentclass command, include any necessary packages and be as small as possible to demonstrate your problem. At the moment we have to guess what packages etc you are using before we can compile your code.
– user30471
Jan 17 '20 at 9:36

Edit II

The code below gives my interpretation of a more efficient way of doing what the OP is asking for but given the final comment it seems that I should just just fixed the original code, without trying to improve it, for which it is enough to replace the \labelrec command in the OP with

\makeatletter
\newcommand*{\labelrec}[2]{%
\begingroup
\xdef\@currentlabel{#2 (Section \noexpand\ref{Sec#1})}%
\label{#1}%
\endgroup
#2%
}
\makeatother


This assumes that as in the OP the subsections are given labels of the form Sec#1, where #1 is the label given to \labelrec.

Original solution

I would do this a little differently and, instead, define a \Subsection command that takes two arguments:

\Subsection{section name}{text to be repeated}


This command starts the (sub)section, adds the "text to be repeated", remembers it and then automatically adds a \label. In addition, there is a second command, \Conclusions, that starts the conclusions section and repeats all of the text.

With this is place the code

  \Section{Analysis}
\Subsection{Assessment 1 Conclusion}{First example of repeated text}

\Subsection{Assessment 2 Conclusion}{Second example of repeated text}

\Conclusions


produces

The hyperlinks are created using hyperref, with coloured blue links, and the "Section" is added to the \subsection reference by the adding the lines

\usepackage[colorlinks, linkcolor=blue]{hyperref}
\renewcommand\subsectionautorefname{Section}


and then using \autoref, instead of \ref, to create the references.

Under the hood I am using LaTeX3 sequences to remember and then regurgitate, the repeated text. Here is the full code:

\documentclass{article}

\usepackage{xparse}
\ExplSyntaxOn
\seq_new:N \g_section_text_seq
\NewDocumentCommand\Subsection{ mm }
{
\subsection{#1} % start the subsection
\label{subsection\arabic{subsection}} % automatically add a label
#2
% finally remember the text together with a hyperlink
\seq_put_right:Nx \g_section_text_seq { #2~(\exp_not:N \autoref{ subsection\arabic{subsection} }) }
}
\NewDocumentCommand\Conclusions{}
{
% start the conclusions section and repeat the text
\Section{Conclusions}
\seq_map_inline:Nn \g_section_text_seq {##1\\}
}
\ExplSyntaxOff

\let\Section\section
\renewcommand\subsectionautorefname{Section}

\begin{document}

\Section{Analysis}
\Subsection{Assessment 1 Conclusion}{First example of repeated text}

\Subsection{Assessment 2 Conclusion} {Second example of repeated text}

\Conclusions

\end{document}


Note that the code in the OP creates labels for both the subsection and the repeated text whereas the code above creates only one label as is sufficient from my understanding of what you are trying to do.

In the absence of a minimal working example, I do not know what \Section is supposed to do, so I have used \section instead.

EDIT I

Here is an modified version of the orignal code in light of the comments below. It is good that you mentioned you are using scrartcl as this impacts the code. To implement the changes requested in the comments there are now two commands:

1. \conconlusion: for adding a conclusion to a subsection. This both adds the conclusion to the subsection and "remembers" it so that it can be automatically repeated at the top of the conclusions section.

2. \Conclusions: this starts the conclusions section and prints the conclusions from the subsection.

With this in place, an expanded version of the following code

  \section{Analysis I}
\subsection{Assessment 1 Conclusion}
\conclusion{First example of repeated text}
\subsection{Assessment 2 Conclusion}
\conclusion{Second example of repeated text}
Some text in between
\conclusion{Third example of repeated text}
\Conclusions


produces the following output:

The code uses scrartcl "hooks" to automatically add subsection labels, so there is no need to do this by hand. As requested, the links in the conclusion point to the start of the corresponding subsection. If required it would be possible to make the each link point to the corresponding conclusion, rather than to the subsection header, but this would require a little more work.

Here is the updated code:

\documentclass[english, fontsize=11pt, DIV=12, twoside, numbers=noendperiod]{scrartcl}
% Section Spacing
\RedeclareSectionCommand[beforeskip=-1sp,afterskip=12pt]{section}
\RedeclareSectionCommand[beforeskip=-1sp,afterskip=12pt]{subsection}
\RedeclareSectionCommand[beforeskip=-1sp,afterskip=12pt]{subsubsection}
\RedeclareSectionCommand[afterskip=12pt]{paragraph}
\RedeclareSectionCommand[afterskip=12pt]{subparagraph}

\newcommand\secnumindent{2cm}
\RedeclareSectionCommands[indent=-\secnumindent]{section,subsection,subsubsection,paragraph,subparagraph}

\renewcommand\sectionformat{\parbox{\secnumindent}{\thesection\autodot}}
\renewcommand\subsectionformat{\parbox{\secnumindent}{\thesubsection\autodot}}
\renewcommand\subsubsectionformat{\parbox{\secnumindent}{\thesubsubsection\autodot}}
\renewcommand\paragraphformat{\parbox{\secnumindent}{\theparagraph\autodot}}
\renewcommand\subparagraphformat{\parbox{\secnumindent}{\thesubparagraph\autodot}}

% add automatic subsection labels using \xpatch
\usepackage{xpatch}
\xapptocmd\subsection\labelsubsection{}{}
\newcommand\labelsubsection{\label{subsection\thesubsection}}

\usepackage{xparse}
\ExplSyntaxOn
\seq_new:N \g_conclusion_text_seq % sequence for holding the conclusions
\NewDocumentCommand\conclusion{ m }
{
#1 % print the text and add to \g_conclusion_text_seq
\seq_put_right:Nx \g_conclusion_text_seq { #1~(\exp_not:N \autoref{ subsection\thesubsection }) }
}
\NewDocumentCommand\Conclusions{}
{
% start the conclusions section and repeat the text
\section{Conclusions}
\seq_map_inline:Nn \g_conclusion_text_seq {##1\\}
\seq_gclear:N \g_conclusion_text_seq
}
\ExplSyntaxOff

% hyperref should be loaded last
\renewcommand\subsectionautorefname{Section}

\begin{document}

\section{Analysis I}
\subsection{Assessment 1 Conclusion}

\conclusion{First example of repeated text}

\subsection{Assessment 2 Conclusion}

\conclusion{Second example of repeated text}

Some text in between

\conclusion{Third example of repeated text}

\Conclusions

\section{Analysis II}
\subsection{Assessment 1 Conclusion}

\conclusion{Another First example of repeated text}

\subsection{Assessment 2 Conclusion}

\conclusion{Another Second example of repeated text}

Some text in between

\conclusion{Another Third example of repeated text}

\Conclusions

\end{document}


With an update-to-date version of scrartcls rather than using \xapptocmd to add the section labels you can use the in-built hook proved by the document class:

% add automatic subsection labels using \AddtoDoHook


I just noticed that the MWE has changed since the initial post and that text can now appear between the Conclusions heading and the repeated conclusions. Given this it makes more sense to define

\NewDocumentCommand\Conclusions{}
{
% repeat the conclusions
\seq_map_inline:Nn \g_conclusion_text_seq {##1\\}
\seq_gclear:N \g_conclusion_text_seq
}


and to insert the \section{Conclusions} heading manually.

• Hi Andrew, thanks for the above. This is almost it except in each Analysis section (e.g. Assessment 1 Conclusion) there will be some text which isn't repeated and then some other text that is repeated and then in the Conclusions section there will similarly be some standalone introductory text and then the repeated text. I've updated my question with the MWE I'm using, fixed the \Section mistake I'd made and then hopefully clarified it a little more. Jan 18 '20 at 9:19
• @ScottMcQuaid I won't have time to look at this until tonight. Just to make sure that I know what you want, does each subsection has just one chunk of repeated text? Also, can there be more than one set of analysis...conclusions in the one document?
– user30471
Jan 18 '20 at 9:27
• Hi Andrew, each subsection will likely have more than one chunk of repeated text and there will also be more than one set of analysis and conclusions section in the one document. Thanks again for your help. Jan 18 '20 at 15:02
• @ScottMcQuaid I have updated the code to cater for your comments
– user30471
Jan 19 '20 at 12:10
• Hi Andrew, unfortunately I can't get your code to compile (I'm using XeLaTeX on Overleaf for this document). I have an error on line 22 (\AddtoDoHook{heading/begingroup/subsection}{\labelsubsection}) which is an undefined control sequence error. Jan 19 '20 at 16:30