2

I am trying to list data from a csv file using csvsimple and to add automatic hyperlinks to related sections.

Issue

When working with a file with a header (but using the no head option to define a different behaviour for the header), the row count is incremented by 1 which shifts the indexes accordingly.

This causes undefined references.

Question

Is there a way to somehow use (\thecsvrow - 1) instead of the actual row number?

Do I absolutely have to manually modify my indexes to match the counter?

MWE

\documentclass[draft]{article}

\usepackage{mwe}
\usepackage{booktabs}
\usepackage{csvsimple}
\usepackage{hyperref}

\begin{filecontents*}{data.csv}
name,type,value
var1,numeric,32+1
var2,string,"text"
var3,float,3.14-x
\end{filecontents*}

\begin{document}

\section{Variables}\label{sec:var}

\csvloop{
  file=data.csv,
  respect all,
  separator=comma,
  no head,
  column names={1=\variableName, 2=\variableType, 3=\variableValue},
  before reading={
    \begin{table}
      \centering
    \begin{tabular}{llr}
    \toprule
  },
  command={\csviffirstrow
    {\textbf{\variableName} & \textbf{\variableType} & \textbf{\variableValue}}
    {\hyperref[sec:var-\thecsvrow]{\variableName} & \variableType & \variableValue}
  },
  late after line=\\,
  late after first line=\\\midrule,
  late after last line=\\\bottomrule,
  after reading={
    \end{tabular}
    \caption{List of variables}
    \label{tab:variables}
    \end{table}
  }
}

\subsection{Variable 1}\label{sec:var-1}
\dots

\subsection{Variable 2}\label{sec:var-2}
\dots

\subsection{Variable 3}\label{sec:var-3}
\dots

\end{document}
1

Playing with the csvrow counter seems to work.

I've subtracted 1 from the counter before the \hyperref and then restored to the original value adding 1 (otherwise it'd always have the value of 1):

\addtocounter{csvrow}{-1}\hyperref[sec:var-\thecsvrow]{\variableName}\addtocounter{csvrow}{1}

Please note that I removed the option [draft] from your \documentclass command, otherwise the links don't work, and, if you use Adobe Acrobat Reader DC, you have to enlarge the document to see the links working (at least at 60%, since the sub sections are empty).

\documentclass{article}

\usepackage{filecontents}
\usepackage{booktabs}
\usepackage{csvsimple}
\usepackage{hyperref}

\begin{filecontents*}{data.csv}
    name,type,value
    var1,numeric,32+1
    var2,string,"text"
    var3,float,3.14-x
\end{filecontents*}

\begin{document}
    
    \section{Variables}\label{sec:var}
    
    \csvloop{
        file=data.csv,
        respect all,
        separator=comma,
        no head,
        column names={1=\variableName, 2=\variableType, 3=\variableValue},
        before reading={
            \begin{table}
                \centering
                \begin{tabular}{llr}
                    \toprule
                },
                command={\csviffirstrow
                    {\textbf{\variableName} & \textbf{\variableType} & \textbf{\variableValue}}
                    {\addtocounter{csvrow}{-1}\hyperref[sec:var-\thecsvrow]{\variableName}\addtocounter{csvrow}{1} & \variableType & \variableValue}
                },
                late after line=\\,
                late after first line=\\\midrule,
                late after last line=\\\bottomrule,
                after reading={
                \end{tabular}
                \caption{List of variables}
                \label{tab:variables}
            \end{table}
        }
    }
    
    \subsection{Variable 1}\label{sec:var-1}
    \dots
    
    \subsection{Variable 2}\label{sec:var-2}
    \dots
    
    \subsection{Variable 3}\label{sec:var-3}
    \dots
    
\end{document}

Clicking on var1 here:

enter image description here

You go to Variable 1 subsection:

enter image description here

and so on.

1
  • Clever solution. Thank you for taking the time to explain and format your answer clearly. – Akaizoku Dec 6 '17 at 17:14

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.