0

I want to create a table with requirements for a project.

   \begin{table}[H]
      \centering
      \begin{tabularx}{\textwidth}{C{0.1}L{0.75}C{0.15}}
         \toprule
            \textbf{R\hyp{}ID} & \textbf{Requirement Description} & \textbf{Priority} \\
         \midrule
            R1 & Requirement 1 & 1 \\
            R2 & Requirement 2 & 1 \\
         \bottomrule
      \end{tabularx}
      \caption{Ziele}
      \label{tab:goals}
   \end{table}

What I wish to do now is, that the R1, R2 are labelled and can be referenced from the text with e.g. \cref{requirement-1}.

The command to create the R1 / R2 entries should be customizable: \uniqueReference{requirement}{R} where requirement would be the label prefix and R the text prefix.

   \begin{table}[H]
      \centering
      \begin{tabularx}{\textwidth}{C{0.1}L{0.75}C{0.15}}
         \toprule
            \textbf{R\hyp{}ID} & \textbf{Requirement Description} & \textbf{Priority} \\
         \midrule
            \uniqueReference{requirement}{R} & Requirement 1 & 1 \\
            \uniqueReference{requirement}{R} & Requirement 2 & 1 \\
         \bottomrule
      \end{tabularx}
      \caption{Ziele}
      \label{tab:goals}
   \end{table}

Is there a package which does this already? Or would I have to create a custom command?

Pascal
  • 779

1 Answers1

1

I have found an answer.

First, check this question about Reference table row in LaTeX.

user31729 used a counter for both rows and cols. If you want a global unique ref counting, just remove the resetting mechanism at each start of environment aka \AtBeginEnvironment{tabular}{\setcounter{rowcntr}{0}}

You then could just define a new counter, such as:

\newcounter{rowcntr}
\renewcommand{\therowcntr}{\arabic{rowcntr}}

% A new columntype to apply automatic stepping \newcolumntype{N}{>{\refstepcounter{rowcntr}}c}

I hope this accomplishes what you wanted to do. Here is the MWE code:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage[danish]{babel}
\usepackage{lscape}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{float} 
\usepackage{array}
\usepackage[top=2.81cm, bottom=2.75cm,right=2cm, left=2cm]{geometry}

\usepackage{etoolbox} \usepackage{setspace} \onehalfspacing

\usepackage{url} \usepackage[hidelinks]{hyperref}

\hypersetup{breaklinks=true}

\newcounter{rowcntr} \renewcommand{\therowcntr}{\arabic{rowcntr}}

% A new columntype to apply automatic stepping \newcolumntype{N}{>{\refstepcounter{rowcntr}}c}

% Reset the rowcntr counter at each new tabular %\AtBeginEnvironment{tabular}{\setcounter{rowcntr}{-1}}

\begin{document}

\begin{table}[H]
    \centering
    \caption{Table with questions} \label{tab:Q1}
    \begin{tabular}{|N|c|l|l|}
        \hline
          Theory & Question                         & Explanation                  \\ \hline
        \label{que:whyisit}           B    & Why is it you think ...          & This is a good question      \\
        \label{que:doyouthink}        A    & Do you think  ...                & This Is also a good question \\
        \label{que:isitcorrect}       B    & is it correct that you think ... & This is question             \\ \hline
    \end{tabular}
\end{table}

Question \ref{que:whyisit} is about ... \\
Question \ref{que:doyouthink} is about ... \\
Question \ref{que:isitcorrect} is about ... 


\newpage

\begin{table}[H] \centering \caption{Table with questions} \label{tab:Q2} \begin{tabular}{|N|c|l|l|} \hline Theory & Question & Explanation \ \hline \label{que:whyisit2} B & Why is it you think ... & This is a good question \ \label{que:doyouthink2} A & Do you think ... & This Is also a good question \ \label{que:isitcorrect2} B & is it correct that you think ... & This is question \ \hline \end{tabular} \end{table}

Question \ref{que:whyisit2} is about ... Question \ref{que:doyouthink2} is about ... Question \ref{que:isitcorrect2} is about ... \end{document}

dexteritas
  • 9,161
anis
  • 1,510
  • 5
  • 13