1

I have a table where I am dynamically loading values. For now, just a single value for testing, but all the numbers here will eventually be loaded.

\documentclass{article} \usepackage{booktabs} \usepackage{threeparttable} \usepackage{amsmath} \usepackage{datatool} \DTLloaddb{baseline}{data.csv} \usepackage{siunitx} % For number formatting \sisetup{ round-mode=places, round-precision=3, % 3 decimal places table-format=-1.3, % Format for negative numbers with 1 digit before decimal, 3 after table-alignment-mode=format, table-number-alignment=center, input-signs=, % Accept both - and − for minus input-decimal-markers={.}, group-separator={,}, group-minimum-digits=4 } \begin{document} \begin{table}[htbp] \centering \begin{threeparttable} \caption{Model Calibration and Moments} \label{tab:model_calibration_moments} \begin{tabular}{ cl| S[table-format=-1.3]| S[table-format=-1.3]S[table-format=-1.3]| S[table-format=-1.3]S[table-format=-1.3] } \toprule \multicolumn{7}{l}{\textbf{Panel A: Calibrated Parameters}} \\ \toprule \multicolumn{3}{c|}{} & \multicolumn{2}{c|}{Baseline Calibration} & \multicolumn{2}{c}{Alternative Calibration} \\ \midrule \multicolumn{3}{l|}{Common parameters} & & & & \\ $c$ & \multicolumn{2}{l|}{Vacancy search cost} & \multicolumn{2}{c|}{ \num{\DTLfetch{baseline}{parameter}{c}{value}}} & \multicolumn{2}{c}{0.0042} \\ $\alpha$ & \multicolumn{2}{l|}{Matching elasticity} & \multicolumn{2}{c|}{\DTLfetch{baseline}{parameter}{alphaMatch}{value}} & \multicolumn{2}{c}{0.4800} \\ $\sigma_z$ & \multicolumn{2}{l|}{Productivity dispersion} & \multicolumn{2}{c|}{\DTLfetch{baseline}{parameter}{z_std}{value}} & \multicolumn{2}{c}{0.1200} \\ $A$ & \multicolumn{2}{l|}{Asset supply} & \multicolumn{2}{c|}{\DTLfetch{baseline}{parameter}{asset_net_supply}{value}} & \multicolumn{2}{c}{0.5500} \\ $\rho$ & \multicolumn{2}{l|}{Discount rate} & \multicolumn{2}{c|}{0.0025} & \multicolumn{2}{c}{0.0030} \\ \midrule \multicolumn{3}{l|}{Type-specific parameters} & {Stable} & {Marginal} & {Stable} & {Marginal} \\ $b_i$ & \multicolumn{2}{l|}{Income of unemployed} & 1.025 & 0.697 & 1.050 & 0.710 \\ $z_i$ & \multicolumn{2}{l|}{Log productivity} & 0.000 & -0.252 & 0.000 & -0.270 \\ $h_i$ & \multicolumn{2}{l|}{Utility from non-employment} & 0.000 & 0.309 & 0.000 & 0.325 \\ $\delta_i$ & \multicolumn{2}{l|}{Exogenous sep. rate} & 0.005 & 0.030 & 0.006 & 0.035 \\ \midrule \end{tabular} \end{threeparttable} \end{table} \end{document} 

with the data file containing

parameter,value c,0.024123123 

The problem is that siunitx formatting (-1.3) does not work in multicolumn by default. Apparently, it does work if I wrap it around with \num{0.123123123}. However, If the numbers are dynamically loaded, it again won't work. (In my example, the one loaded value is simply blank in the table).

How could I approach this?

2
  • What's in the file data.csv? Commented Apr 27 at 17:50
  • @Mico I added the minimum necessary data Commented Apr 27 at 17:54

2 Answers 2

1

The problem is that siunitx formatting (-1.3) does not work in \multicolumn by default.

The remedy is to use S rather than c as the column type. I therefore suggest you (a) use S rather than c in the second argument of the \multicolumn directives and (b) get rid of the \num wrappers in the third argument of the \multicolumn directives.

Oh, and don't use vertical rules when employing the booktabs package. No exceptions. No way, no how.

The following table employs only one active \DTLfetch instruction as the OP provided only one line's worth of data for the file data.csv.

enter image description here

\documentclass{article} \begin{filecontents*}[overwrite]{data.csv} parameter,value c,0.024123123 \end{filecontents*} \usepackage{booktabs} \usepackage{threeparttable} \usepackage{amsmath} \usepackage{datatool} \DTLloaddb{baseline}{data.csv} \usepackage{siunitx} % For number formatting \sisetup{ round-mode=places, round-precision=3, % 3 decimal places table-format=1.3, % table-alignment-mode=format, % table-number-alignment=center, % input-signs=, % Accept both - and − for minus % input-decimal-markers={.}, % group-separator={,}, % group-minimum-digits=4 } \begin{document} \begin{table}[htbp] \centering \begin{threeparttable} \caption{Model Calibration and Moments} \label{tab:model_calibration_moments} \smallskip \begin{tabular}{@{} >{$}l<{$} l *{2}{S S[table-format=-1.3]} @{}} \toprule \multicolumn{6}{@{}l}{Panel A: Calibrated Parameters} \\ \midrule && \multicolumn{2}{c}{Baseline} & \multicolumn{2}{c@{}}{Alternative} \\ && \multicolumn{2}{c}{Calibration} & \multicolumn{2}{c@{}}{Calibration} \\ \cmidrule(lr){3-4} \cmidrule(l){5-6} \multicolumn{2}{@{}l}{Common parameters}\\ c & Vacancy search cost & \multicolumn{2}{S}{\DTLfetch{baseline}{parameter}{c}{value}} & \multicolumn{2}{S@{}}{0.0042} \\ \alpha & Matching elasticity & %\multicolumn{2}{S}{\DTLfetch{baseline}{parameter}{alphaMatch}{value}} && \multicolumn{2}{S@{}}{0.4800} \\ \sigma_z & Productivity dispersion & %\multicolumn{2}{S}{\DTLfetch{baseline}{parameter}{z_std}{value}} && \multicolumn{2}{S@{}}{0.1200} \\ A & Asset supply & %\multicolumn{2}{S}{\DTLfetch{baseline}{parameter}{asset_net_supply}{value}} && \multicolumn{2}{S@{}}{0.5500} \\ \rho & Discount rate & \multicolumn{2}{S}{0.0025} & \multicolumn{2}{S@{}}{0.0030} \\ \addlinespace %\midrule \multicolumn{2}{@{}l}{Type-specific parameters} & {Stable} & {Marginal} & {Stable} & {Marginal} \\ \cmidrule(lr){3-4} \cmidrule(l){5-6} b_i & Income of unemployed & 1.025 & 0.697 & 1.050 & 0.710 \\ z_i & Log productivity & 0.000 & -0.252 & 0.000 & -0.270 \\ h_i & Utility from non-employment & 0.000 & 0.309 & 0.000 & 0.325 \\ \delta_i & Exogenous sep.\ rate & 0.005 & 0.030 & 0.006 & 0.035 \\ \bottomrule % \midrule % why '\midrule'? \end{tabular} \end{threeparttable} \end{table} \end{document} 
3
  • @FooBar - I have MacTeX2025 on my system; I used pdfLaTeX to compile this test document. If you're getting a lot of Undefined control sequence error messages, you must not have copied over the full preamble to your file. Certainly don't write \multicolumn{S}, though; it's got to be \multicolumn{2}{S}{...}. Note also that I'm setting table-format=1.3 as the default, not table-format=-1.3. Commented Apr 27 at 21:33
  • Now that I managed to get this to work, I realize that my test case was pretty silly: the input data was only 3 numbers post decimal, so siunitx really had nothing to do. Could you try it with the updated data? I'm asking because while the code runs, it doesn't seem to do the formatting as expected. Commented Apr 28 at 8:41
  • I'm afraid your new request exceeds my zone of competency. Very sorry. Please post a new query with a simplified setup, to focus on the interactions of the datatool and siunitx packages. That way, many more people -- in particular, hopefully, the maintainers of these fine packages -- will have a chance to think about the issue and, hopefully, come up with a solution. Commented Apr 28 at 9:10
0

I am sure a better solution will come forward. I could not find a documentation for \DTLfetch in datatool docs, so I had to look at a trace and do some guesswork. I am sure it has a variant to store the result of \dtlcurrentvalue in some macro rather than print it.

edit I see it now on page 445 of datatool-code.pdf wher its definition is given. One could copy this definition and simply remove the token \dtlcurrentvalue from it at its end, to get a simpler method than the dummy box used below.

\documentclass{article} \usepackage{booktabs} \usepackage{threeparttable} \usepackage{amsmath} \begin{filecontents*}{data.csv} parameter,value c,0.024 \end{filecontents*} \usepackage{datatool} \DTLread[name=baseline]{data.csv} \usepackage{siunitx} % For number formatting \sisetup{ round-mode=places, round-precision=3, % 3 decimal places table-format=-1.3, % Format for negative numbers with 1 digit before decimal, 3 after table-alignment-mode=format, table-number-alignment=center, input-signs=, % Accept both - and − for minus input-decimal-markers={.}, group-separator={,}, group-minimum-digits=4 } \begin{document} \begin{table}[htbp] \centering \begin{threeparttable} \caption{Model Calibration and Moments} \label{tab:model_calibration_moments} \begin{tabular}{ cl| S[table-format=-1.3]| S[table-format=-1.3]S[table-format=-1.3]| S[table-format=-1.3]S[table-format=-1.3] } \toprule \multicolumn{7}{l}{\textbf{Panel A: Calibrated Parameters}} \\ \toprule \multicolumn{3}{c|}{} & \multicolumn{2}{c|}{Baseline Calibration} & \multicolumn{2}{c}{Alternative Calibration} \\ \midrule \multicolumn{3}{l|}{Common parameters} & & & & \\ $c$ & \multicolumn{2}{l|}{Vacancy search cost} & \multicolumn{2}{c|}{\setbox0\hbox{ \DTLfetch{baseline}{parameter}{c}{value}\xdef\TEMP{\dtlcurrentvalue} }\num{\TEMP}} & \multicolumn{2}{c}{0.0042} \\ % $\alpha$ & \multicolumn{2}{l|}{Matching elasticity} & \multicolumn{2}{c|}{\DTLfetch{baseline}{parameter}{alphaMatch}{value}} & \multicolumn{2}{c}{0.4800} \\ % $\sigma_z$ & \multicolumn{2}{l|}{Productivity dispersion} & \multicolumn{2}{c|}{\DTLfetch{baseline}{parameter}{z_std}{value}} & \multicolumn{2}{c}{0.1200} \\ % $A$ & \multicolumn{2}{l|}{Asset supply} & \multicolumn{2}{c|}{\DTLfetch{baseline}{parameter}{asset_net_supply}{value}} & \multicolumn{2}{c}{0.5500} \\ $\rho$ & \multicolumn{2}{l|}{Discount rate} & \multicolumn{2}{c|}{0.0025} & \multicolumn{2}{c}{0.0030} \\ \midrule \multicolumn{3}{l|}{Type-specific parameters} & {Stable} & {Marginal} & {Stable} & {Marginal} \\ $b_i$ & \multicolumn{2}{l|}{Income of unemployed} & 1.025 & 0.697 & 1.050 & 0.710 \\ $z_i$ & \multicolumn{2}{l|}{Log productivity} & 0.000 & -0.252 & 0.000 & -0.270 \\ $h_i$ & \multicolumn{2}{l|}{Utility from non-employment} & 0.000 & 0.309 & 0.000 & 0.325 \\ $\delta_i$ & \multicolumn{2}{l|}{Exogenous sep. rate} & 0.005 & 0.030 & 0.006 & 0.035 \\ \midrule \end{tabular} \end{threeparttable} \end{table} \end{document} 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.