1

I'm currently running into an issue where I'll only get the titles of my columns showing for my table when using csvsimple with \csvautotabular. table problem

The current code I used to produce this is

    \documentclass{report}
    \usepackage[utf8]{inputenc}
    \usepackage{csvsimple}
    
    \begin{document}
    \csvautotabular[respect all]{test_utf8.csv}
    \end{document}

I have used the test.csv

    Name,Battery %,Last updated
    L1 A / B (C),5%,"Sept. 1, 2021, 00:00 a.m."
    G D,12%,"Aug. 31, 2021, 9:09 a.m."
    L2 E / F (G),80%,"Feb. 21, 2021, 10:00 a.m."

which I believe has the general formatting required for my other csv files.

2 Answers 2

0

Two changes solve the problem:

  1. Changing \csvautotabular[respect all]{test_utf8.csv} to \csvautotabular[respect percent]{test_utf8.csv} (and other special characters that should be interpreted as normal characters).

  2. Hiding commas which are not separators:

    Name,Battery %,Last updated
    L1 A  B (C),5%,{Sept. 1, 2021, 0000 a.m.}
    G D,12%,{Aug. 31, 2021, 909 a.m.}
    L2 E  F (G),80%,{Feb. 21, 2021, 1000 a.m.}
    

Because you need curly braces to mask a block that may contain commas not to be processed as separators, you shouldn't choose respect all.

enter image description here

6
  • Thank you for the quick response. So besides the change of use for the respect function, it's just a formatting issue in the csv itself? Currently I'm pasting data into excel and removing what columns/rows I don't need and saving as .csv. There's probably a quick way to replace the quotation marks with curly braces before I save to import into TeX. Sep 1, 2021 at 6:52
  • @JacksonWright Yes, commas were used in two functions. If you are expecting many commas in your data, you can choose another separator in CSV files by, say, [separator=semicolon] as an option to \csvautobooktabular. Sep 1, 2021 at 6:57
  • @JacksonWright I have just corrected the wrong percent sings in my answer. They certainly should be commas. Sep 1, 2021 at 7:02
  • Cheers, the hardest part now is just getting a program to save it with curly brackets instead of quote marks hahaha :) Sep 1, 2021 at 7:12
  • @JacksonWright Or omitting quote marks with ; as a separator. Sep 1, 2021 at 7:21
0

I thought I would take this space to show off the recently added (v3.0 2021-08-08) capability of the readarray package to create tabulars. Because the file contains raw % tokens and uses quote grouping "...", I required a little more setup to digest it.

Basically, the \readdef macro places the contents of a file into a \def, while the \readarray macro places the contents of the \def into an array.

The \typesetarray macro is newly introduced, and uses helper macros named \typesetrowsepchar, \typesetcolsepchar (also \typesetplanesepchar for 3-D) to format the typeset, as they are inserted between rows, columns, and/or planes.

EDITED to place the catcode stuff in its own macro, easing the burden on invocation setup. Thus, the \readdef invocation is replaced with \readdefquotespercents in order to automatically digest raw % tokens as characters and to use quote-grouping.

RE-EDITED to use \lowercase magic to define the active character " inside of a macro.

\begin{filecontents*}[overwrite]{junk.dat}
Name,Battery %,Last updated
L1 A / B (C),5%,"Sept. 1, 2021, 00:00 a.m."
G D,12%,"Aug. 31, 2021, 9:09 a.m."
L2 E / F (G),80%,"Feb. 21, 2021, 10:00 a.m."
\end{filecontents*}
\documentclass{article}
\usepackage{readarray}[2021-08-08]
\readarraysepchar{,}
\renewcommand\typesetrowsepchar{\\\hline}
\renewcommand\typesetcolsepchar{&}

\newcommand\readdefquotespercents[2]{%
  \catcode`\"=\active
  \begingroup\lccode`\~=`"
  \lowercase{\endgroup\def~##1~}{{##1}}%
  \catcode`\%=12 \readdef{#1}#2\catcode`\%=14 
  \catcode`\"=12 
  \edef\tabdata{\tabdata}% CONVERT QUOTED PHRASES
}
\begin{document}
\readdefquotespercents{junk.dat}\tabdata

\readarray\tabdata\tabarray[-,\ncols]% PLACE def INTO ARRAY

\begin{tabular}{|c|c|c|}
\hline
\typesetarray\tabarray\\
\hline
\end{tabular}
\end{document}

enter image description here

3
  • The length is too short to post the full error details but I'm getting the following errors with your code on Overleaf. LaTeX Error: Command \typesetrowsepchar undefined. LaTeX Error: Command \typesetcolsepchar undefined. Undefined control sequence. ! Use of \tabarray doesn't match its definition. l.28 \typesetarray\tabarray\\ Sep 3, 2021 at 5:27
  • @JacksonWright The readarray package seems outdated. The one needed was released a month ago, [2021-08-08], v3.0 Sep 3, 2021 at 11:40
  • @JacksonWright see ctan.org/pkg/readarray Sep 3, 2021 at 11:46

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.