# Refer to “[Section], [Paragraph]” when [Paragraph] is custom formatted

I would like to have sections and single number paragraphs, like

2.3.1 Frobnication
§1 Foo bar frob


which I achieved. But when when I refer to the paragraph ...

\myref{Paragraph's Label}


... but all I could achieve looks like:

See §1 (p. 1).


But I would like to have it like so:

See Section 2.3.1, §1 (p.1)


At the moment, this is my ref-command and my renewed \theparagraph:

\setcounter{secnumdepth}{5}
\renewcommand\theparagraph{§\arabic{paragraph}}
\newcommand{\pararef}[1]{\ref{#1} (p. \pageref{#1})}


My current workaround is to have two parameters to \pararef, but it feels unclean and clumsy.

-
Hmm, maybe I can refine the section-command to define some user-command to be used within my custom ref-macro. –  phresnel Mar 30 '12 at 14:31
What about the package scrjura which offers such references? –  Keks Dose Mar 30 '12 at 15:57
@KeksDose: I've tried it; I think it is too intrusive for my purposes. However, I will at some point write a language-standard for a tiny programming language I've written (which is part of a bigger program (picogen.org)), and I think there it has a good place :) –  phresnel Apr 2 '12 at 16:49

You can define a "reference prefix": if foo is a counter, then its value when a label refers to it is printed as

\p@foo\thefoo


where \p@foo is by default empty. So

\documentclass{article}
\setcounter{secnumdepth}{5}
\renewcommand\theparagraph{\S~\arabic{paragraph}}
\newcommand{\pararef}[1]{\ref{#1} (p.~\pageref{#1})}

\makeatletter
\renewcommand{\p@paragraph}{\thesubsubsection, }
\makeatother

\begin{document}
\section{A}
\subsection{B}
\subsubsection{C}
\paragraph{D}\label{x}

This is the reference: \pararef{x}
\end{document}


If you don't always use \paragraph subordinate to subsubsections, then you can say

\makeatletter
\renewcommand{\p@paragraph}{%
\ifnum\value{subsection}=\z@
\thesection
\else
\ifnum\value{subsubsection}=\z@
\thesubsection
\else
\thesubsubsection
\fi
\fi,\space
}
\makeatother


This assumes that \paragraph is at least subordinate to a section. This is a test document

\documentclass{article}
\setcounter{secnumdepth}{5}
\renewcommand\theparagraph{\S~\arabic{paragraph}}
\newcommand{\pararef}[1]{\ref{#1} (p. \pageref{#1})}

\makeatletter
\renewcommand{\p@paragraph}{%
\ifnum\value{subsection}=\z@
\thesection
\else
\ifnum\value{subsubsection}=\z@
\thesubsection
\else
\thesubsubsection
\fi
\fi,\space
}
\makeatother

\begin{document}
\section{A}
\subsection{B}
\subsubsection{C}
\paragraph{D}\label{x}

This is the reference: \pararef{x}

\section{E}
\paragraph{F}\label{y}

This is the reference: \pararef{y}

\section{G}
\subsection{H}
\paragraph{I}\label{z}

This is the reference: \pararef{z}
\end{document}


I've always found this kind of cross references heavy and not friendly to the reader.

-
+1, thanks. I like it for its minimalism; do you have more information on this prefix-thingy? And, can this also be made flexible enough when a paragraph is a descendant of section/subsection? So as to print 3 § 1 (p.1) (instead of 3.0.1 § 1 (p.1)) –  phresnel Mar 30 '12 at 15:06
@phresnel I've added the code as requested. However, I would never recommend such a style. –  egreg Mar 30 '12 at 15:45
I see. I think then it would be better to define my own paragraph-command. Note that this is for technical documentation purposes where I will need some cross-references. Thanks again for your answer. –  phresnel Apr 2 '12 at 16:51

I'll leave my original answer below so the comments make sense but a better solution is to use the built in prefixing mechanism for cross references (that by default isn't used for sections but is for nested lists). The command \p@paragraph is used at the start of any cross reference text (but mot when the counter is directly printed in the heading)

\documentclass{article}
\makeatletter

\setcounter{secnumdepth}{5}
\renewcommand\theparagraph{\P\arabic{paragraph}}
\newcommand{\pararef}[1]{\ref{#1} (p. \pageref{#1})}

\def\p@paragraph{Section \@currentlabel\ }

\makeatother

\begin{document}

\section{jhg}
\paragraph{jj}\label{jj}
\subsection{ljhg}
\subsubsection{lkb ljhg}
\paragraph{jjj}\label{foo}

jhgg

\section{ojh}
See \pararef{foo} and \pararef{jj}

\end{document}


One way is to put the long form into \theparagraph but not print it all when making the heading:

\documentclass{article}

\setcounter{secnumdepth}{5}
\renewcommand\theparagraph{\protect\myswitch{Section \thesubsubsection\ }\P\arabic{paragraph}}
\newcommand{\pararef}[1]{\ref{#1} (p. \pageref{#1})}
\makeatletter
\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}%
{3.25ex \@plus1ex \@minus.2ex}%
{-1em}%
{\let\myswitch\@gobble\normalfont\normalsize\bfseries}}

\def\myswitch#1{#1}
\makeatother

\begin{document}

\section{jhg}
\subsection{ljhg}
\subsubsection{lkb ljhg}
\paragraph{jjj}\label{foo}

jhgg

\section{ojh}
See \pararef{foo}

\end{document}

-
+1, thanks. Can this also be made flexible enough when a paragraph is a descendant of section/subsection? So as to print 3 § 1 (p.1) (instead of 3.0.1 § 1 (p.1)) –  phresnel Mar 30 '12 at 15:05
well possibly but LaTeX's sectioning model really assumes that each sectioning command comes at a fixed level, \paragraph is supposed to be used as a sus-sub-sub-section not as a unit that may appear at any level. Basically just replace \thesubsubsection by \@currentlabel in the definition of \theparagraph (move the definition after \makeatletter ) and it will pick up whatever is around.... –  David Carlisle Mar 30 '12 at 15:15
What about a \paragraph following another \paragraph, with the new code? The reference will be wrong, because the first \paragraph sets \@currentlabel. –  egreg Mar 30 '12 at 18:11
@egreg good point, that's the use of \@currentlabel rather than \thesubsubsection yes as I said in my 1st comment, latex's sectioning doesn't really support using the same sectioning command at different levels. Should either use \thesubsubsection or make sure each paragraph is in its own group so that \@currentlabel picks up the parent not the preceding paragraph. Either way I think using \p@paragraph is better than my first suggestion. –  David Carlisle Mar 30 '12 at 18:48