0

Essentially, what I wish to do is fully reference the numbers of an object's part, chapter and section using only the label of the object itself (and not the labels of the part, and section). I have the following commands with its own counter (that resets with every new section):

\newcounter{important}[section]

\newcommand{\imp}[3]{\refstepcounter{important} \todo[inline, color=red!30,caption={}]{\hypertarget{#1}{\underline{Important (#2) \thepart.\thesection.\theimportant $\longrightarrow$ \label{#1}}} #3}}

\newcommand{\impref}[3]{\hyperlink{#1}{Important \ref{#2}.\ref{#3}.\ref{#1}}}

I can use them like this

\usepackage[colorinlistoftodos]{todonotes}
\usepackage[colorlinks=true, linkcolor=blue]{hyperref}

\newcounter{important}[section]
\newcounter{importantref}[section]

\newcommand{\imp}[3]{\refstepcounter{important} \todo[inline, color=red!30,caption={}]{\hypertarget{#1}{\underline{Important (#2) \thepart.\thesection.\theimportant $\longrightarrow$ \label{#1}}} #3}}

\newcommand{\impref}[3]{\hyperlink{#1}{Important \ref{#2}.\ref{#3}.\ref{#1}}}


\begin{document}

\part{this is a part} \label{pt: partlabel1}

\chapter{this is a chapter} 

\section{this is a section} \label{sec: seclabel1}

\imp{important: label}{Brief name}{description}

\part{another part} \label{pt: partlabel2}

\chapter{another chapter}

\section{another section} \label{sec: seclabel2}

Here I am referencing this thing from another part chapter and section \impref{important: label}{pt: partlabel1}{sec: seclabel1}


\end{document}

Which produces the following output:

enter image description here

Right now, my code does what I want. As you can see, I have successfully fully referenced the part, chapter and section number of the object as well as the object's counter itself.;so you might be wondering okay.. so what's the problem?

Well my issue is that every time I want to reference the object's part, chapter and section using \impref I have to go and manually find the labels of the part and section the object is in as well, and input those labels into the command \impref. What I wish to do is to somehow (I believe) create a new type of label that automatically stores the part number and section number that the object was created in when the label is created for the object, so that when I want to reference it later, the only information I need to input into \impref is the label of the object itself so that the \impref command may be reduced to one that requires only a single input, rather than three.

  • Something like \makeatletter \renewcommand*{\p@important}{Important~} \makeatother \renewcommand*{\theimportant}{\thepart.\thesection.\arabic{important}} \newcommand{\imp}[3]{\refstepcounter{important} \todo[inline, color=red!30,caption={}]{\hypertarget{#1}{\underline{Important (#2) \theimportant $\longrightarrow$ \label{#1}}} #3}} \newcommand{\impref}[3]{\hyperlink{#1}{\ref{#1}}} should work – moewe Jun 28 '18 at 14:34
  • 1
    Why don't you try to use \thepart \thechapter and \thesection? – koleygr Jun 28 '18 at 14:34
1

You can use \p@important to prefix a string before the counter and use \thepart.\thesection. in \theimportant directly.

Most of the custom link targets and links are probably not needed, so you can get away with

\documentclass{report}
\usepackage[colorinlistoftodos]{todonotes}
\usepackage[colorlinks=true, linkcolor=blue]{hyperref}

\newcounter{important}[section]
\makeatletter
\renewcommand*{\p@important}{Important~}
\makeatother
\renewcommand*{\theimportant}{\thepart.\thesection.\arabic{important}}

\newcommand{\imp}[3]{%
  \refstepcounter{important}\label{#1}%
  \todo[inline, color=red!30,caption={}]{\underline{Important (#2) \theimportant $\longrightarrow$ }#3}}

\begin{document}
\part{this is a part}
\chapter{this is a chapter} 
\section{this is a section} 
\imp{important:label}{Brief name}{description}

\part{another part}
\chapter{another chapter}
\section{another section}
Here I am referencing this thing from another part chapter and section \ref{important:label}
\end{document}

enter image description here

enter image description here


If you want to include the name of the \imp the best solution I could find was

\documentclass{report}
\usepackage[colorinlistoftodos]{todonotes}
\usepackage[colorlinks=true, linkcolor=blue]{hyperref}

\newcounter{important}[section]
\renewcommand*{\theimportant}{\thepart.\thesection.\arabic{important}}
\makeatletter
\newcommand{\imp}[3]{%
  \refstepcounter{important}\def\@currentlabelname{#2}\label{#1}%
  \todo[inline, color=red!30,caption={}]{\underline{Important (#2) \theimportant $\longrightarrow$ }#3}}
\makeatother

\newcommand*{\impref}[1]{\hyperref[{#1}]{Important (\nameref*{#1}) \ref*{#1}}}

\begin{document}
\part{this is a part}
\chapter{this is a chapter} 
\section{this is a section} 
\imp{important:label}{Brief name}{description}

\part{another part}
\chapter{another chapter}
\section{another section}
Here I am referencing this thing from another part chapter and section \impref{important:label}
\end{document}

enter image description here

Unfortunately, this means you need to use the dedicated \impref command instead of the generic \ref. See also \nameref -- How to display section name AND its number

  • A nice, that's excellent. You have redefined the counter to store additional information? – Decebalus Jun 28 '18 at 14:43
  • Additionally, what if I wish to include the "brief name" that was input into \imp, such that the output is instead: Important (Brief name) A.1.1.1 – Decebalus Jun 28 '18 at 14:57
  • @Decebalus Yes, or rather: Its printed representation. \the<foo> defines how the counter <foo> is normally displayed. – moewe Jun 28 '18 at 14:57
  • @Decebalus I will have a look, but if it is too complicated, I might ask you to ask a new question instead. – moewe Jun 28 '18 at 14:58
  • @Decebalus See the updated answer. – moewe Jun 28 '18 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.