2

I've got an own environment for requirements. In my document I want to reference each requirement and get its text. If I use \namref I get only the name of the corresponding section. Can someone help me?

Here is a short example

\documentclass[12pt,ngerman,a4paper,oneside]{scrbook}
\usepackage[paper=a4paper,left=25mm,right=25mm,top=20mm,bottom=20mm,includeheadfoot]{geometry}
\usepackage{hyperref}




%% Requirement environment
\newcounter{requirement}
\renewcommand\therequirement{A\arabic{requirement}} 
\newcommand{\reqref}[1]{\ref{#1}}
\newenvironment{req}[1][]{
    \refstepcounter{requirement}
    \begin{list}{\textbf{A\arabic{requirement}}\label{#1}}{%
            \setlength{\labelsep}{10pt}\setlength{\leftmargin}{35pt}%
            \setlength{\labelwidth}{40pt}\setlength{\listparindent}{0pt}%
        }\em\item}%
    {\end{list}}





\begin{document}

\chapter{My Chapter}
\section{My Section}

\begin{req}[A10]
    Text First requirement...
\end{req}

\begin{req}[A20]
    Text First requirement...
\end{req}

\begin{req}[A30]
    Text First requirement...
\end{req}


Now I want to reference the requirement and the its text. \\
\nameref{A10}  \\
\nameref{A20} \\
\nameref{A30} \\


\end{document}
4
  • 2
    Welcome to TeX.SE! Can you please complete your code snippet to be compilable? Then we do not have to guess what you are doing ...
    – Mensch
    Aug 30 '19 at 13:42
  • Try \edef\@currentlablename{...} after \refstepcounter and before \label in order to get {...} from \nameref. You will need \makeatletter ... \makeatother. Aug 30 '19 at 14:18
  • To be clear, \nameref{A10} should return Text First requirement..., correct? Will you always want to use \nameref after the requirement environment you reference (never before)? Are you open to using a different interface? That is, not \label-\nameref?
    – Werner
    Aug 30 '19 at 18:58
  • @Werner yes \nameref{A10} should return Text First requirement.... I would use \nameref only for this environment and I am open to use a different interface. I only need sometimes to reference the number of the requirement. For this i use \ref. In the above example \ref{A10} should return A10.
    – jSh4rk
    Aug 31 '19 at 7:29
2

The following example uses environ to capture the contents of the req environment (in \BODY), which is written to the .aux in the same way regular \labels are written (by updating \@currentlabel before calling \label).

enter image description here

\documentclass{scrbook}

\usepackage{hyperref,environ}

\newcounter{requirement}
\renewcommand{\therequirement}{A\arabic{requirement}}

\makeatletter
\newcommand{\reqref}[1]{\ref{#1}}
\newcommand{\textref}[1]{\ref{t@#1}}
\NewEnviron{req}[1]{%
  \refstepcounter{requirement}%
  \begin{list}{\textbf{\therequirement}\label{#1}}{%
      \setlength{\labelsep}{10pt}\setlength{\leftmargin}{35pt}%
      \setlength{\labelwidth}{40pt}\setlength{\listparindent}{0pt}%
    }\itshape\item
    \def\@currentlabel{\BODY}\label{t@#1}%
    \BODY
  \end{list}
}
\makeatother

\begin{document}

\chapter{My Chapter}
\section{My Section}

\begin{req}{A10}
  Text first requirement\ldots
\end{req}

\begin{req}{A20}
  Text second requirement\ldots
\end{req}

\begin{req}{A30}
  Text third requirement\ldots
\end{req}

Now I want to reference the requirement and the its text: 
\textref{A10}

\end{document}

Note that writing content to the .aux is fragile, as everything is expanded. As such, there may be some cases where you need to \protect whatever you write within the body of the req environment.

In this example, I've made the argument of req mandatory since you're using it as a \label. This \label has to be unique, and leaving it empty (or missing, in the case of an optional argument) would result in duplicate \labels.

Finally, I've changed the interface to \textref using the same label supplied to req.

0

I found a quick and dirty way to get the text with \nameref. Therefor I use the solution from here:

Labeling a text and referencing it later

Here is the complete doc:

\documentclass[12pt,ngerman,a4paper,oneside]{scrbook}
\usepackage[paper=a4paper,left=25mm,right=25mm,top=20mm,bottom=20mm,includeheadfoot]{geometry}
\usepackage{hyperref}


\newcounter{requirementtextcounter}
\makeatletter
\newcommand{\labelText}[2]{%
    #1\refstepcounter{requirementtextcounter}%
    \immediate\write\@auxout{%
        \string\newlabel{#2}{{1}{\thepage}{{\unexpanded{#1}}}{requirementtextcounter.\number\value{requirementtextcounter}}{}}%
    }%
}
\makeatother


\newcounter{requirement}
\renewcommand\therequirement{A\arabic{requirement}} 
\newcommand{\reqref}[1]{\ref{#1}}
\newenvironment{req}[1][]{
    \refstepcounter{requirement}
    \begin{list}{\textbf{A\arabic{requirement}}\label{#1}}{%
            \setlength{\labelsep}{10pt}\setlength{\leftmargin}{35pt}%
            \setlength{\labelwidth}{40pt}\setlength{\listparindent}{0pt}%
        }\em\item}%
    {\end{list}}


\begin{document}

\chapter{My Chapter}
\section{My Section}

\begin{req}[A10]
    \labelText{Text First requirement...}{AT10}
\end{req}

\begin{req}[A20]
    Text First requirement...
\end{req}

\begin{req}[A30]
    Text First requirement...
\end{req}


Now I want to reference the requirement and the its text. \\
\nameref{AT10}



\end{document}

Output

1
  • Don't add the example as answer, edit your question instead. Aug 30 '19 at 15:53

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.