2

The csvtools package warns in its documentation from interpretable inputs but offers no solution as far as I can see.

This Code generates a table

\documentclass{article}
\usepackage{csvtools}

\begin{document}

\newcommand{\DATARow}[4]{#1 & #2 & #3 & #4\\[.5ex]}

\setcsvseparator{;}

\CSVtotabular%
{stackoverflow.csv}%
{|c|c|c|c|}%
{\DATARow{ID}{REGISTER}{TYPE}{FORMULA}}%
{\DATARow{\insertID}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}%
{\DATARow{\insertID}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}

\end{document}

using this file as a source:

ID;REGISTER;TYPE;FORMULA
A_B_C_D;0xD000720C;UINT(16);$Y = \frac{57426}{10^8} \cdot X$
ONE;0xD020720C;UINT(8);$Y = 109 \cdot X$
TWO.FOUR;0xD080720C;INT(16);$Y = \frac{57426}{X}$
Thirty[2];0xD009720C;INT(8);$Y = X$
TreeHouse;0xD200720C;UINT(32);$Y = \frac{X}{10^8} \cdot X$

The problem is in the ID string: A_B_C_D it should be printed as is but gets interpreted as math and printed like $A_{BCD}$

How can i modify the DATARow so that A_B_C_D is shown correctly?

2 Answers 2

3

enter image description here

You can use \detokenize to make _ safe and T1 encoding and \texttt to display it literally.

\documentclass{article}
\usepackage{csvtools}
\usepackage[T1]{fontenc}
\begin{document}

\newcommand{\DATARow}[4]{#1 & #2 & #3 & #4\\[.5ex]}
\def\verbid{%
\texttt{\edef\tmp{\insertID}\expandafter\detokenize\expandafter{\tmp}}}

\setcsvseparator{;}

\CSVtotabular%
{stackoverflow.csv}%
{|c|c|c|c|}%
{\DATARow{ID}{REGISTER}{TYPE}{FORMULA}}%
{\DATARow{\verbid}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}%
{\DATARow{\verbid}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}

\end{document}
3

You have a number of possibilities. All of the following solutions allow for using _ in formulas with the usual meaning. My preference would go to the third one.

Making _ active

\documentclass{article}
\usepackage{csvtools}

\newcommand{\DATARow}[4]{#1 & #2 & #3 & #4\\[.5ex]}
\setcsvseparator{;}

%%% Make _ active
\catcode`\_=\active
\protected\def_{\ifmmode\sb\else\_\fi}
%%%

\begin{document}

\CSVtotabular{stackoverflow.csv}%
  {|c|c|c|c|}%
  {\DATARow{ID}{REGISTER}{TYPE}{FORMULA}}%
  {\DATARow{\insertID}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}%
  {\DATARow{\insertID}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}

\end{document}

Making _ a normal character and math active globally

Note that the T1 encoding is necessary for this solution

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{csvtools}

\newcommand{\DATARow}[4]{#1 & #2 & #3 & #4\\[.5ex]}
\setcsvseparator{;}

%%% Make _ a normal character and math active
\catcode`\_=12
\AtBeginDocument{\mathcode`\_=\string"8000 }
\begingroup\lccode`\~=`\_
\lowercase\endgroup{\let~\sb}
%%%

\begin{document}

\CSVtotabular{stackoverflow.csv}%
  {|c|c|c|c|}%
  {\DATARow{ID}{REGISTER}{TYPE}{FORMULA}}%
  {\DATARow{\insertID}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}%
  {\DATARow{\insertID}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}

\end{document}

Make _ a character and math active only locally

Again the T1 encoding is necessary.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{csvtools}

\newcommand{\DATARow}[4]{#1 & #2 & #3 & #4\\[.5ex]}
\setcsvseparator{;}

%%% Make _ a normal character and math active
\newcommand{\specialunderscore}{%
  \catcode`\_=12
  \mathcode`\_=\string"8000
  \begingroup\lccode`\~=`\_
  \lowercase\endgroup{\let~\sb}}

%%% Define a wrapper around \CSVtotabular
\newcommand{\xCSVtotabular}[6][]{%
  \begingroup#1\CSVtotabular{#2}{#3}{#4}{#5}{#6}\endgroup}

%%%

\begin{document}

\xCSVtotabular[\specialunderscore]{stackoverflow.csv}%
  {|c|c|c|c|}%
  {\DATARow{ID}{REGISTER}{TYPE}{FORMULA}}%
  {\DATARow{\insertID}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}%
  {\DATARow{\insertID}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}

\end{document}

The \xCSVtotabular accepts an optional argument that should consist of assignments such as \specialunderscore or similar ones that locally change some meaning of tokens.

You must log in to answer this question.

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