2

I'd like to create the following table using datatool:

enter image description here

\documentclass{article}

\begin{document}

\begin{tabular}{ *{3}{r} }
  \bfseries FieldA & \bfseries FieldB & \bfseries FieldC \\
  First & Second & Third \\
  \textit{First} & \textbf{Second} & \textsl{Third} \\
  First\&A & Second\_B & Third
\end{tabular}

\end{document}

In this example the second row contains a bunch of formatting commands (\textbf, \textit, \textsl, ...) that I'd like to keep, while the third row contains some active characters I'd like to print as-is (that is, & > \&, _ > \_, ...).

I thought I could use datatool's \DTLloadrawdb to get around the active character mapping, but this doesn't work with the formatting commands as illustrated in the following example:

\documentclass{article}

\usepackage{datatool,filecontents}

\begin{filecontents*}{data.csv}
FieldA, FieldB, FieldC
First, Second, Third
\textit{First}, \textbf{Second}, \textsl{Third}
First&A, Second_B, Third
\end{filecontents*}

\DTLloadrawdb{data}{data.csv}% ... booooom!

\begin{document}

\DTLdisplaydb{data}

\end{document}

How can I leave some commands within a database untouched (like the formatting commands \textbf, \textit, ... and their arguments), while still retaining the \DTLrawmap feature for certain active characters (like &, _, ...)?

2 Answers 2

3

enter image description here

\documentclass{article}

\usepackage{datatool,filecontents}

\begin{filecontents*}{data.csv}
FieldA, FieldB, FieldC
First, Second, Third
\textit{First}, \textbf{Second}, \textsl{Third}
First&A, Second_B, Third
\end{filecontents*}

{\catcode`\&=12 \catcode`\_=12
\DTLloaddb{data}{data.csv}% ... booooom!
}

\usepackage[T1]{fontenc}

\begin{document}

\DTLdisplaydb{data}

\end{document}
2

\DTLloadrawdb makes all the special chars active, and it uses \xdef. So imho it should be used with much care. If you want to insert a special char (here: braces) you must use commands instead. You should prevent premature expansion. And commands at end of the line can be problematic too.

In your case this here compiles. The quotes are only needed for the last cell, and also only because it ends with \egroup.

\documentclass{article}

\usepackage{datatool}
\usepackage{filecontents}

\begin{filecontents*}{data.csv}
FieldA, FieldB, FieldC
"\noexpand\textit\bgroup First\egroup", "\noexpand\textbf\bgroup Second\egroup", "\noexpand\textsl\bgroup Third \egroup"
abc,abc,abc
\end{filecontents*}

\DTLloadrawdb{data}{data.csv}

\begin{document}

\DTLdisplaydb{data}

\end{document}

enter image description here

6
  • I examined a trace and (as expected) the \bgroup ends up as argument to \textbf so this does not look right. Or is the after-effect a devilishly composed trick? Why not \noexpand\bfseries?
    – user4686
    Nov 7, 2017 at 10:30
  • @jfbu The question was about \textbf ;-). And with \bfseries you would need a way to group it too. Also while I didn't do a trace "\noexpand\textit\bgroup First\egroup aaaa " does work. Nov 7, 2017 at 10:34
  • take this on joking side: there are thousands of imprecations on this side about not using \rm, \bf (which do have their quite useful purposes), so \textit\bgroup foo\egroup although working (but not executing \check@icr at the right place) scares me as being definitely not politically correct ;-)
    – user4686
    Nov 7, 2017 at 11:20
  • @jfbu ;-). But I must say I'm more scared about the \xdef in \DTLloadrawdb ;-). Nov 7, 2017 at 11:32
  • +1 I will now brag about my recently acquired science: couldn't \DTLloadrawdb use \protected@xdef ? at least this would make robust commands such as \textbf behave properly.
    – user4686
    Nov 7, 2017 at 12:55

You must log in to answer this question.

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