# Need LaTeX to automatically update a reference number within a table

I'm need to assign a reference number inside a table such that it automatically updates the number at the end and restarts the counter when the initial part of the reference number changes. For example, I might have FX-##, and I want LaTeX to enumerate FX-01, FX-02 etc. I might then change to EL-## and I need LaTeX to reset the counter as I've started a new reference number, so it would become EL-01. I also want to be able to label the reference numbers and refer to them later. When I refer to them later I want the full reference number to show, FX-01 etc. I do not know where to start on this so have included very basic code.

\documentclass{article}
\begin{document}
\begin{table}[]
\begin{tabular}{|l|l|l|}
FX-01\label{fire}  & Fire & Call for help \\
FX-02\label{explo} & Explosion  & Run away \\
EL-01\label{elec} & Electrocution & Contact doctor  \\
\end{tabular}
\end{table}
\end{document}

• You could have started with a compilable document at least, not only fragments. \label needs a counter to be connected to, unless you know the magic behind it. – user31729 Feb 8 '19 at 11:30
• I've been criticized before for adding in too much code and have been asked to only include the minimum required to understand the problem. Can't please everyone. – gravity121 Feb 8 '19 at 11:31
• Typing \documentclass and \begin{document} ...\end{document} + your table stuff would be the minimal version. – user31729 Feb 8 '19 at 11:32

Something like this?

My proposition uses categories (such as FX or EL), each time a new category is introduced, a counter is increased and the local element counter is reset. The \newcatelement macro is responsible for stepping the element counter, an eventual label and the output of FX-XX where XX is a the number.

\documentclass{article}

\usepackage{xparse}

\newcounter{categorycounter}
\newcounter{elementcounter}[categorycounter]

\makeatletter

\renewcommand{\theelementcounter}{%
\@currentcategory-%
\ifnum\value{elementcounter} <9\relax%
0\number\value{elementcounter}%
\else
\arabic{elementcounter}%
\fi
}

\newcommand{\newcategory}[1]{%
\gdef\@currentcategory{#1}%
\refstepcounter{categorycounter}%
}
\NewDocumentCommand{\newcatelement}{+o}{%
\refstepcounter{elementcounter}%
\IfValueT{#1}{\label{#1}}%
\theelementcounter%
}

\makeatother

\usepackage{hyperref}

\begin{document}

\begin{table}[]
\begin{tabular}{|l|l|l|}
\newcategory{FX}\newcatelement[fire]  & Fire & Call for help \\
\newcatelement[explo] & Explosion  & Run away \\
\newcategory{EL}\newcatelement[elec] & Electrocution & Contact doctor  \\
\end{tabular}
\end{table}

See \ref{fire} or \ref{explo}, but also \ref{elec}
\end{document}


Update with better setup

\documentclass{article}

\usepackage{xcolor}
\usepackage{xparse}

\newcounter{categorycounter}
\newcounter{elementcounter}[categorycounter]

\makeatletter

\newcommand{\@categoryseparator}{-}

\NewDocumentCommand{\setcategoryseparator}{+m}{%
\renewcommand{\categoryseparator}{#1}%
}

\renewcommand{\theelementcounter}{%
\@currentcategory\@categoryseparator%
\ifnum\value{elementcounter} <9\relax%
0%
\fi
\arabic{elementcounter}%
}

\def\@currentcategory{}

\NewDocumentCommand{\newcategory}{m}{%
\gdef\@currentcategory{#1}%
\refstepcounter{categorycounter}%
\ignorespaces
}

\NewExpandableDocumentCommand{\newcatelement}{s+o+m+m}{%
\refstepcounter{elementcounter}%
\edef\@currentlabelname{#4}%
\IfValueT{#2}{\label{#2}}%
\theelementcounter& #3 & #4 \IfBooleanF{#1}{\tabularnewline}%
}

\makeatother

\usepackage{hyperref}

\begin{document}

\begin{table}[]
\begin{tabular}{|*{3}{l|}}
\newcategory{FX}
\newcatelement[fire]{Fire}{Call for help}%
\newcatelement[explo]{Explosion}{Run away}%
\newcategory{EL}
\newcatelement[elec]{Electrocution}{Contact doctor}
\end{tabular}
\end{table}

In case of \ref{fire} just \colorbox{yellow}{\nameref*{fire}} or but in case of \ref{explo} \colorbox{red}{\color{white}\bfseries\nameref*{explo}}. Take care of \ref{elec}!
\end{document}


• Absolutely lovely. Thank you for your help. Works perfectly. – gravity121 Feb 8 '19 at 15:20
• @gravity121: You're welcome! Happy TeXing! – user31729 Feb 10 '19 at 19:03