Graphics, Figures & Tablessiunitx | Dollar Sign in Table

Information and discussion about graphics, figures & tables in LaTeX documents.
UniquarkJ
Posts: 6
Joined: Sat Nov 26, 2011 6:51 am

siunitx | Dollar Sign in Table

Post by UniquarkJ »

I'm having trouble with dollar signs in the input for a table using the S column. My table has some cells with dollar amounts and some cells without dollar amounts, so I can't put the dollar signs outside the cells. However, when I put the dollar sign (\$) in the cell with the number, it seems to stop siunitx from processing the rest of the number. Here's a simple example

Code: Select all

\begin{tabular*}{\textwidth}{@{ }l@{\extracolsep{\fill}}S[table-format=5.4]S[table-format=3.7]@{ }}
    \hline	
    \multicolumn{1}{c}{Variable} & \multicolumn{1}{c}{Weighted mean} & \multicolumn{1}{c}{Standard error} \\ \hline
    Poverty in county        & 11.01646   & .3431699    \\
    Median family income     & \$45041.39 & \$728.0975  \\ 
    Income ratio             & .8478574   &  .0106983   \\\hline
\end{tabular*}
The result I get has the numbers in the Median family income row fully left justified in the cells. In addition, the thousands separator is not being added to the number in the second column. Dropping the currency symbol (\$) eliminates the problem. In case it matters, I'm running TeXLive 2011 in Snow Leopard on a MacBook Pro.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

siunitx | Dollar Sign in Table

Post by localghost »

Please always prepare a minimal example that reproduces the undesired behaviour and is compilable out of the box for everybody. Otherwise specific help becomes difficult and in case of more complicated issues almost impossible.

You can try the »table-space-text-pre« option for the concerned columns as introduce in Section 5.14 of the siunitx manual.

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}
  \noindent
  \begin{tabular*}{\textwidth}{
    @{ }
    l@{\extracolsep{\fill}}
    S[table-format=5.7,table-space-text-pre={\$}]
    S[table-format=3.7,table-space-text-pre={\$}]
    @{ }
  }\toprule
    \multicolumn{1}{c}{Variable} & {Weighted mean} & {Standard error} \\ \midrule
    Poverty in county            & 11.01646   & .3431699    \\
    Median family income        & \$45041.39 & \$728.0975  \\
    Income ratio                 & .8478574   &  .0106983   \\ \bottomrule
  \end{tabular*}

  \bigskip
  \noindent
  \begin{tabular*}{\textwidth}{@{ }l@{\extracolsep{\fill}}S[table-format=5.7]S[table-format=3.7]@{ }}\toprule
    \multicolumn{1}{c}{Variable} & {Weighted mean} & {Standard error} \\ \midrule
    Poverty in county            & 11.01646   & .3431699    \\
    Median family income (in \$)   & 45041.39 & 728.0975  \\
    Income ratio                 & .8478574   &  .0106983   \\ \bottomrule
  \end{tabular*}
\end{document}
The second table has only been added just to show an alternative that might be a bit clearer for the reader.


Best regards and welcome to the board
Thorsten
UniquarkJ
Posts: 6
Joined: Sat Nov 26, 2011 6:51 am

Re: siunitx | Dollar Sign in Table

Post by UniquarkJ »

Thanks for your help, Thorsten. Unfortunately, adding the extra space in front of the number does not address the problems. The decimal separators are still not aligned (although they are much closer) and the large number is not getting the thousands separator. Another partial solution I have found is the \SI macro. Using that macro inside the cell correctly formats the number itself, but it still does not align the decimal separators. Fortunately, it does come close because the only numbers with the dollar signs are larger than any other numbers in the column.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

siunitx | Dollar Sign in Table

Post by localghost »

UniquarkJ wrote:[…] Unfortunately, adding the extra space in front of the number does not address the problems. The decimal separators are still not aligned (although they are much closer) and the large number is not getting the thousands separator. […]
Yes, indeed. Didn't notice that without magnification. At the moment I have no idea for a workaround.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

siunitx | Dollar Sign in Table

Post by cgnieder »

You're nearly there: first I'd use the correct table format in the second column, i.e. 5.7 instead of 5.4; second hiding the \$ from siunitx by putting it into braces should do the trick:

Code: Select all

\documentclass{article}
\usepackage{array,siunitx}
\begin{document}

\begin{tabular*}{\linewidth-15pt}{@{ }l@{\extracolsep{\fill}}S[table-format=5.7,table-space-text-pre={\$}]S[table-format=3.7,table-space-text-pre={\$}]@{ }}
    \hline   
    \multicolumn{1}{c}{Variable} & \multicolumn{1}{c}{Weighted mean} & \multicolumn{1}{c}{Standard error} \\ \hline
    Poverty in county            & 11.01646                          & .3431699    \\
    Median family income         & {\$} 45041.39                     & {\$}728.0975  \\
    Income ratio                 & .8478574                          &  .0106983   \\\hline
\end{tabular*}

\end{document}
BTW: I noticed that the table produces a overfull \hbox (15pt with the article class or 11pt with the scrartcl class). This seems to be an error with the tabular* environment, since this

Code: Select all

\begin{tabular*}{\linewidth}{c}
 A
\end{tabular*}
gives the same overfull \hbox.
That is why I used "\linewidth-15pt" in the example above.

Best
site moderator & package author
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

siunitx | Dollar Sign in Table

Post by localghost »

cgnieder wrote:[…] I noticed that the table produces a overfull \hbox (15pt with the article class or 11pt with the scrartcl class). This seems to be an error with the tabular* environment, since this

Code: Select all

\begin{tabular*}{\linewidth}{c}
 A
\end{tabular*}
gives the same overfull \hbox.
That is why I used "\linewidth-15pt" in the example above. […]
Remember that the table is just indented by the \parindent length value. A \noindent right before (like I did in my example) lets the overfull box disappear.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Re: siunitx | Dollar Sign in Table

Post by cgnieder »

Stupid me! Of course... thanks anyway!
site moderator & package author
UniquarkJ
Posts: 6
Joined: Sat Nov 26, 2011 6:51 am

Re: siunitx | Dollar Sign in Table

Post by UniquarkJ »

Thanks for the help, Clemens. Your solution is almost perfect and it lets me get at least this table working even though it does not work quite the way I need in general. The problem I'm still experiencing is that siunitx wants to put the dollar signs way out at the edge of the largest possible number (as defined by the table-format option). Fortunately, my table has just one row of dollar values and each one is the largest value in its respective column.

Interestingly, removing the table-format option helps some, but then the numbers are centered on the decimal marker. Also, when the value has more digits to the right of the decimal marker than to the left of it, the currency symbol is still not closed up on the number.

I'm aware that some people like to align the currency symbols independently of the number alignment, but that practice does not conform to all style guides. It would be nice if siunitx could meet this need without having to trick it.
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

siunitx | Dollar Sign in Table

Post by josephwright »

You can turn off alignment of text before the number:

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}
\begin{table}
\sisetup{table-space-text-pre=\$,table-align-text-pre = false}
\begin{tabular}
  {lS[table-format=5.4]S[table-format=3.7]}
  \toprule   
    \multicolumn{1}{c}{Variable} &{Weighted mean} & {Standard error} \\
  \midrule
    Poverty in county        & 11.01646     & .3431699    \\
    Median family income     & {\$}45041.39 & {\$}728.0975  \\
    Extra row!               & {\$}5041.39  & {\$}28.0975  \\
    Income ratio             & .8478574    &  .0106983   \\
    \bottomrule
\end{tabular}
\end{table}
\end{document}
This is not a 'trick', it's simply that the S column is for numbers and so 'other' material has to be handled separately.
Joseph Wright
UniquarkJ
Posts: 6
Joined: Sat Nov 26, 2011 6:51 am

Re: siunitx | Dollar Sign in Table

Post by UniquarkJ »

Thanks for the help, Joseph. This appears to be exactly what I was seeking. However, while I can make table-align-text-post, the version of siunitx I'm using is rejecting table-align-text-pre. Is this a new feature that would not be available in TexLive 2011?

When I try to process your code verbatim, I get the following message.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! siunitx error: "unknown-option"
!
! Unknown option 'table-align-text-pre'.
!
! See the siunitx documentation for further information.
! For immediate help type H <return>.
!...............................................
Post Reply