Search code examples
latexnewenvironment

Latex - Custom Reference to Custom Environment


I want to do the following thing in Latex:

I have a custom environment like this:

\documentclass{article}
\usepackage{hyperref}
\usepackage{tabularx}
\title{Item Table Test}

\newcounter{ItemDescriptionCnt}

\makeatletter 
\renewcommand{\theItemDescriptionCnt}{Item \@arabic\c@ItemDescriptionCnt}
\makeatother

\newenvironment{ItemDescription}[5][]{\refstepcounter{ItemDescriptionCnt}\par\medskip
    \begin{tabularx}{\textwidth}{|>{\hsize=.2\hsize}X|>{\hsize=.6\hsize}X|>{\hsize=.2\hsize}X|}
        \hline
        \textbf{Item No.}   & \textbf{Titel}   & \textbf{Type}          \\
        \theItemDescriptionCnt    &  #2   & Item                \\
        \hline          
        \multicolumn{3}{|l|}{\textbf{Description:}}     \\  
        \multicolumn{3}{|p{\dimexpr\linewidth-2\tabcolsep-2\arrayrulewidth}|}{#3}                               \\
        \hline
        \multicolumn{2}{|l|}{\textbf{Note:} } & \textbf{Status:}    \\
        \multicolumn{2}{|>{\hsize=.8\hsize}X|}{#4} & #5             \\
        \hline  
    \end{tabularx}
    \noindent \rmfamily}{\medskip}

\newcommand{\itemdescription}[5]{
    \begin{ItemDescription}{#1}{#2}{#3}{#4}
        #5
    \end{ItemDescription}
}

\begin{document}

\itemdescription{This is the title}{This is the description.}{This is the note.}{pending}{\label{e}}

Here I reference \ref{e}.

\end{document}

This means I can then do

\itemdescription{This is the title}{This is the description.}{This is the note.}{pending}{\label{e}}

Here I reference \ref{e}.

Here you can see the generated file: enter image description here

Which I already like! Now I would like to customize the reference.

I would like to do:

This is long reference: \longref{e}

Which should result in:

This is long reference: Item 1 - This is the title (pending)

I sadly can not figure this out -.- I hope someone knows how to do that. Thanks in advance!

Bant


Solution

  • I figured it out. In case somebody want to do something similar I want to add how I got it doing what I want it to do.

    The key is to use:

    
    \expandafter\def\csname myitem\theItemDescriptionCnt\endcsname{#1}
    
    

    inside

    \newcommand{\itemdescription}[5]{
        \begin{ItemDescription}{#1}{#2}{#3}{#4}
            #5
        \end{ItemDescription}
        \expandafter\def\csname myitem\theItemDescriptionCnt\endcsname{#1}
    }
    

    If I understand this correctly (And please correct me if I am wrong), the \expandafter lets the arguments first be "expanded" before the the \def is run. The \csname [...] \endcsname lets you define a constructed command within the enclosure. Then the myitem\theItemDescriptionCnt constructs a command, which in my example looks like this: myitemItem 1, which, when run, refers to the argument {#1}.

    The following command \longref then does pretty much exactly the same. It uses the input to get the reference number. It then puts it together to get the same command (inside the \csname enclosure), which was generated before. The command then outputts the argument given before:

    \newcommand{\longref}[1]{%
        \ref{#1} - \csname myitem\getrefnumber{#1}\endcsname
    }
    

    My complete example looks like this:

    \documentclass{article}
    \usepackage{graphicx} % Required for inserting images
    \usepackage{hyperref}
    \usepackage{tabularx}
    \title{tests}
    \date{January 2024}
    
    \usepackage{aliascnt}
    \usepackage{xstring}
    \usepackage{refcount}
    
    \newcounter{ItemDescriptionCnt}
    
    \makeatletter 
    \renewcommand{\theItemDescriptionCnt}{Item \@arabic\c@ItemDescriptionCnt}
    \makeatother
    
    \newenvironment{ItemDescription}[5][]{\refstepcounter{ItemDescriptionCnt}\par\medskip
        \begin{tabularx}{\textwidth}{|>{\hsize=.2\hsize}X|>{\hsize=.6\hsize}X|>{\hsize=.2\hsize}X|}
            \hline
            \textbf{Item No.}   & \textbf{Titel}   & \textbf{Type}          \\
            \theItemDescriptionCnt    &  #2   & Item                \\
            \hline          
            \multicolumn{3}{|l|}{\textbf{Description:}}     \\  
            \multicolumn{3}{|p{\dimexpr\linewidth-2\tabcolsep-2\arrayrulewidth}|}{#3}                               \\
            \hline
            \multicolumn{2}{|l|}{\textbf{Note:} } & \textbf{Status:}    \\
            \multicolumn{2}{|>{\hsize=.8\hsize}X|}{#4} & #5             \\
            \hline  
        \end{tabularx}
        \noindent \rmfamily}{\medskip}
    
    \newcommand{\itemdescription}[5]{
        \begin{ItemDescription}{#1}{#2}{#3}{#4}
            #5
        \end{ItemDescription}
    
        \expandafter\def\csname myitem\theItemDescriptionCnt\endcsname{#1}
        \expandafter\def\csname mystate\theItemDescriptionCnt\endcsname{#4}
    }
    
    \newcommand{\longref}[1]{%
        \ref{#1} - \csname myitem\getrefnumber{#1}\endcsname\ (\csname mystate\getrefnumber{#1}\endcsname)
    }
    
    \begin{document}
      
    
    \itemdescription{This is the title}{This is the description.}{This is the note.}{pending}{\label{e}}
    
    Here I reference \ref{e}.
    
    Here is the autoref \autoref{e}.
    
    Here is the longref \longref{e}.
    
    
    \end{document}
    

    This generates:

    enter image description here