# How to retain certain LaTeX commands in a datatool database?

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

\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*}

\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 &, _, ...)?

\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
}

\usepackage[T1]{fontenc}

\begin{document}

\DTLdisplaydb{data}

\end{document}


\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*}


• 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 '17 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. – Ulrike Fischer Nov 7 '17 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 '17 at 11:20