Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

all. Suppose I have a csv file (let's call it data.csv):

Solution,mol/L,Volume(mL),delta_v(mL),moles
HNO3,16,5,0.2,3.125E-4
NaOH,6,20.2,0.2,3.367E-3
H2O,1;200,5,2.000E-1
H2SO4,6,5.2;0.2,8.667E-4
HCl,12,10;0.2,8.333E-4

Now, suppose I wanted to use the mhchem package and the siunitx package to help format different columns in data.csv. Specifically I'd like to wrap everything in the first column inside \ce{} (except the first row). So HNO3, would be \ce{HNO3} and thus would display with a subscripted 3. And I'd like to wrap everything in the moles column in \num{}.

Does anyone know of a good way to do this?

share|improve this question

1 Answer 1

This is one way this could be done:

enter image description here

Notes:

  • As cgnieder suggested, one needs to use \expandafter\ce\expandafter{\Solution}.

Code:

\documentclass{article}
\usepackage{datatool}
\usepackage{siunitx}
\usepackage{mhchem}

\usepackage{filecontents}
\begin{filecontents*}{data.csv}
Solution,mol/L,Volume(mL),delta_v(mL),moles
HNO3,16,5,0.2,3.125E-4
NaOH,6,20.2,0.2,3.367E-3
H2O,1,200,5,2.000E-1
H2SO4,6,5.2,0.2,8.667E-4
HCl,12,10,0.2,8.333E-4
\end{filecontents*}

\newcommand{\PrintDTLTable}[1]{% #1 = database
    \begin{tabular}{c c c c c }%
        Solution & mol/L & Volume(mL) & $delta_v$(mL) & moles \\\hline%
    \DTLforeach{#1}{%
        \Solution=Solution,%
        \Mol=Mol,%
        \Volume=Volume,%
        \Delta=Delta,%
        \Moles=Moles%
        }{%
        \expandafter\ce\expandafter{\Solution} & \num{\Mol} & \num{\Volume} & \num{\Delta} & \num{\Moles}\\%
    }%
    \end{tabular}%
}%

\begin{document}
\DTLloaddb[keys={Solution,Mol,Volume,Delta,Moles}]{myDB}{data.csv}

\PrintDTLTable{myDB}
\end{document}
share|improve this answer
    
You need to expand \Solution first to satisfy \ce: \expandafter\ce\expandafter{\Solution} –  cgnieder Jul 5 '12 at 18:09
    
\ce sort of looks through its input token by token before it decides what is an atom, a subscript, a stoichiometric factor,… also everything is typeset in mathmode (choosing \mathrm for atoms, I believe). If the input is not as \ce expects it is often displayed just like setting it between $…$. Not sure what happens without the {}. The input seems to be doubled and then the first token gobbled… –  cgnieder Jul 5 '12 at 18:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.