2

I haven't found any clear and/or functional examples on how to insert a checkbox in a table generated with csvsimple.

For example, in this MWE, how can I modify the code to replace the checkbox strings by real clickable ones please?

\documentclass{article}
\usepackage{csvsimple}

\begin{filecontents*}{grade.csv}
    cola,colb,colc,cold,cole
    Maier,Hans,0,m,checkbox
    Huber,Anna,1,f,checkbox
    Weisbaeck,Werner,3,m,checkbox
\end{filecontents*}

\begin{document}
    \begin{tabular}{l|c}%
        \bfseries ColA & \bfseries ColE% specify table head
        \csvreader[head to column names]{grade.csv}{}% use head of csv as column names
        {\\\hline\cola\ \colb & \cole}% specify your columns here
    \end{tabular}
\end{document}

Thank you so much Jasper I'm very interested by all your solutions. Certainly, I was searching more specifically something like the second one where checkbox names are generated automatically using a counter !

If it's possible for you (and if I may abuse!), I would ask one more think: How to adapt your second second solution when we have a longtable as, for example, in the following MWE:

\documentclass[draft]{article}

\usepackage{booktabs}
\usepackage{longtable}
\usepackage{csvsimple}
\begin{document}
\begin{filecontents*}{test.csv}
  a,b,c
  1,2,3
  4,5,6
\end{filecontents*}
\csvloop{
  file=test.csv,
  respect all,
  separator=comma,
  no head,
  before reading={
    \begin{longtable}{lll}
    \toprule
  },
  command={\csviffirstrow
    {\textbf{\csvcoli} & \textbf{\csvcolii} & \textbf{\csvcoliii}}
    {\csvcoli & \csvcolii & \csvcoliii}
  },
  late after line=\\,
  late after first line=\\\midrule\endhead,
  late after last line=\\\bottomrule,
  after reading=\end{longtable}
}
\end{document}
4
  • I don't quite understand your edit. Where exactly should the checkbox sit? What you essentially need to do is to replace the macro that places the value from the CSV into the cell (such as \csvcoli) by the code for the checkbox (which is something like \CheckBox[name=<name>]{}). Commented Oct 13, 2022 at 14:39
  • For example, you could replace {\csvcoli & \csvcolii & \csvcoliii} by { \CheckBox[name=checkbox\thecsvrow]{} & \csvcolii & \csvcoliii } ... or you could add a new column and place the checbox there ... it all depends on where you wish the checkbox to appear. Commented Oct 13, 2022 at 14:47
  • @JasperHabicht I would be interested to add check box at the end of each row. I tried to test your example and to replace \csvcoli by \CheckBox[name=checkbox\thecsvrow]{} as you mention. A box appears but,however, in this case, it seems not functional. It is not clickable like it was with tabular ... I don't understand why ?
    – Certes
    Commented Oct 13, 2022 at 16:17
  • See the edit to my answer. Commented Oct 13, 2022 at 16:41

1 Answer 1

2

Welcome to TeX.SX! You probably need to tell a bit more about your use case. As far as I understand your question, you are trying to come up with some kind of interactive PDF form. You can use the package hyperref to create such a form with clickable checkboxes.

Note, however, that in an interactive PDF form, it is a good idea that every field (a checkbox is also a field) has a unique name. Otherwise, depending on the further use of the form (for example when exporting the form data), you might encounter unexpected results. In the following example, I therefore changed the text checkbox in your CSV data to checkbox1, checkbox2 etc. to generate unique IDs that I then used as names for the checkboxes in the form.

\documentclass{article}
\usepackage{csvsimple}
\usepackage{hyperref}

\begin{filecontents*}{grade.csv}
    cola,colb,colc,cold,cole
    Maier,Hans,0,m,checkbox1
    Huber,Anna,1,f,checkbox2
    Weisbaeck,Werner,3,m,checkbox3
\end{filecontents*}

\begin{document}
    \begin{Form}
        \begin{tabular}{l|c}%
            \bfseries ColA & \bfseries ColE% specify table head
            \csvreader[head to column names]{grade.csv}{}% use head of csv as column names
            {\\\hline\cola\ \colb & \CheckBox[name=\cole]{}}% specify your columns here
        \end{tabular}
    \end{Form}
\end{document}

enter image description here

You could also generate the needed unique checkbox names automatically using a counter or using \thecsvrow which retuns the current row number, like in the following example (since you would not need any CSV data related to the checkbox at all in this case, I deleted the relevant column):

\documentclass{article}
\usepackage{csvsimple}
\usepackage{hyperref}

\begin{filecontents*}{grade.csv}
    cola,colb,colc,cold
    Maier,Hans,0,m
    Huber,Anna,1,f
    Weisbaeck,Werner,3,m
\end{filecontents*}

\begin{document}
    \begin{Form}
        \begin{tabular}{l|c}%
            \bfseries ColA & \bfseries ColE% specify table head
            \csvreader[head to column names]{grade.csv}{}% use head of csv as column names
            {\\\hline\cola\ \colb & \CheckBox[name=checkbox\thecsvrow]{}}% specify your columns here
        \end{tabular}
    \end{Form}
\end{document}

The output is the same as above.


On second thought, you say that you want to replace the string checkbox by a clickable checkbox. So, maybe you only want a checkbox in those rows where the string checkbox appears in column cole of the relevant CSV data. (Thanks to @Marijn for pointing to this.)

You can make use of the macro \ifcsvstrequal to check for each row whether the contents of the cell is checkbox and, only if yes, place the checkbox:

\documentclass{article}
\usepackage{csvsimple}
\usepackage{hyperref}

\begin{filecontents*}{grade.csv}
    cola,colb,colc,cold,cole
    Maier,Hans,0,m,checkbox
    Huber,Anna,1,f,
    Weisbaeck,Werner,3,m,checkbox
\end{filecontents*}

\def\checkboxstring{checkbox}

\begin{document}
    \begin{Form}
        \begin{tabular}{l|c}%
            \bfseries ColA & \bfseries ColE% specify table head
            \csvreader[head to column names]{grade.csv}{}% use head of csv as column names
            {\\\hline\cola\ \colb & \ifcsvstrequal{\cole}{checkbox}{\CheckBox[name=checkbox\thecsvrow]{}}{}}% specify your columns here
        \end{tabular}
    \end{Form}
\end{document}

The above code would not place a checkbox in the second row:

enter image description here



You later also asked how to adapt the second of the above solutions to a set up where \csvloop is used to create a longtable. It is important to place all fields in the PDF between \begin{Form} and \end{Form} to ensure full functionality of the form fields. Here, we can add these to the before reading and after reading options:

\documentclass{article}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{csvsimple}
\usepackage{hyperref}

\begin{filecontents*}{test.csv}
  a,b,c
  1,2,3
  4,5,6
\end{filecontents*}

\begin{document}
\csvloop{
  file=test.csv,
  respect all,
  separator=comma,
  no head,
  before reading={
    \begin{Form}
    \begin{longtable}{llll}
    \toprule
  },
  command={\csviffirstrow
    {\textbf{\csvcoli} & \textbf{\csvcolii} & \textbf{\csvcoliii} & }
    {\csvcoli & \csvcolii & \csvcoliii & \CheckBox[name=checkbox\thecsvrow]{}}
  },
  late after line={\\},
  late after first line={\\\midrule\endhead},
  late after last line={\\\bottomrule},
  after reading={
    \end{longtable}
    \end{Form}
  }
}
\end{document}

enter image description here

5
  • While it is a good idea to give the name attribute unique values, it is not always necessary, just for displaying the form, checking some boxes, and saving or printing it, you don't need unique names. Only when processing the form with a Submit button or performing other JavaScript functionality (Adobe Reader only) then the name attribute is needed.
    – Marijn
    Commented Oct 13, 2022 at 13:26
  • Also, I'm not 100% sure but it might be that some of the values in the csv for column e are not checkboxes (but maybe textfields, or "don't add a form element"), your answer doesn't account for that.
    – Marijn
    Commented Oct 13, 2022 at 13:29
  • 1
    @Marijn Yes, you are right. Thanks for pointing to this. I usually think that it is cleaner to already care for such things in the very beginning. Maybe, later you wish to export the form data and then it would be nice to have unique identifiers. It is probably just a habit of mine, since I know forms from the HTML world and there, a field without a name is practically useless. I changed the wording of the relevant sentence in my answer. And yes, my answer would not work if not all rows should contain a checkbox. Commented Oct 13, 2022 at 13:29
  • @JasperHabicht Holy something ... I know that we are not suppose to do some comments to say Thank you, but thank you anyway !
    – Certes
    Commented Oct 13, 2022 at 17:03
  • @Certes I, think it is fine to add a thank-you comment, as long as you don't post it as an answer =) You're welcome! Commented Oct 13, 2022 at 17:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .