3

I want to use siunitx package for writing the units in my document. However, just adding the line:

\usepackage{siunitx}

to the document (and without even using the siunitx commands) gives 84 errors of the type: Missing '}'... Missing '$' ... Extra '}' ... and so on.

Without this line the document compiles and generated fine.

I have managed to locate the source causing the problem, which is the use of math code inside a tabularx.

Here is minimal code to compile:

\documentclass[12pt,twoside]{report}
\usepackage{siunitx}
\usepackage{comment}
\usepackage{tabularx}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\newcolumntype{s}{>{\hsize=.25\hsize\centering\arraybackslash}X}

\begin{document}
\begin{center} 
\begin {table}[h]
\caption{Comparison between bla bla bla}
\centering
{
    \renewcommand{\arraystretch}{1.5} % \renewcommand{\arraystretch}{1.5}
    \begin{tabularx}{0.95\linewidth} {Y|s}   
    \hline\hline
    column 1 title & column 2 title \tabularnewline % [0.5cm]  
    \hline%\hline
    cell(1,1) & $\textrm{w}_0$ \tabularnewline
%\begin{comment}
    cell(2,1) & $\frac{\textrm{w}_0}{\sqrt{2}}\approx 0.7\textrm{w}_0$ \tabularnewline
    cell(3,1) & $\frac{\textrm{w}_0}{\sqrt{3}}\approx 0.58\textrm{w}_0$ \tabularnewline
    cell(4,1) & $0.5\textrm{w}_0$ \tabularnewline
    cell(5,1) & $0.46\textrm{w}_0$ \tabularnewline

    \hline
%\end{comment}
    \end{tabularx}
}
\label{tab:comparing_table}
\end{table} 
%
\end{center} 
\end{document}

Now, without the: \usepackage{siunitx} the above table compiles just fine. The problem is with the math code inside the table. The same table with text only also compiles fine.

How can I keep the table with the math in it and with siunitx ???

13
  • 2
    it is presumably a clash with some other package but as you give no indication of your input it is impossible to guess, please try to construct the smallest example that you can that has the error and add to your question. Jan 17 '19 at 18:58
  • 2
    It's more likely that you have some syntax error that you typed at the same time you added the package. Find where in your tex file the first error occurs, and the error will likely be nearby.
    – Teepeemm
    Jan 17 '19 at 19:07
  • It is a thesis document, very long and using a lot of packages. How can I pinpoint which one is cuasing the problem ? ? Jan 17 '19 at 19:17
  • 2
    it is much easier for you to do it than anyone else:-) first start with a document that has your thesis preamble but no content other than the table. Check that gives the error. If it does not, then the error is not in the table...., if it does , delete every usepackage line that is loading packages not used in the table, until you get a small example that shows the error. If you delete a package and the error goes, that is the problem... Jan 17 '19 at 19:23
  • 1
    Oh I was assuming you had an error in a table from siunitx S columns forget "table" but just have whatever paragraph or equation or whatever is causing the error (the error message log will show the filename and line number of the first error, so basically you just need that line plus whatever else you need to make it a valid latex document. Jan 17 '19 at 19:41
2

It seems to me that the problem comes from your custom columntype since using j instead of s works flawless for me. The s is reserved by the siunitx package.

tabularx

\documentclass[12pt,twoside]{report}
\usepackage{siunitx}
\usepackage{comment}
\usepackage{tabularx}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\newcolumntype{j}{>{\hsize=.25\hsize\centering\arraybackslash}X}

\begin{document}
\begin{center} 
\begin{table}[h]
\caption{Comparison between bla bla bla}
\centering
{
    \renewcommand{\arraystretch}{1.5} % 
    \begin{tabularx}{0.95\linewidth}{Y|j}   
    \hline\hline
    column 1 title & column 2 title \tabularnewline % [0.5cm]  
    \hline%\hline
    cell(1,1) & {$\textrm{w}_0$}  \tabularnewline
    cell(2,1) & $\frac{\textrm{w}_0}{\sqrt{2}}\approx 0.7\textrm{w}_0$ \tabularnewline
    cell(3,1) & $\frac{\textrm{w}_0}{\sqrt{3}}\approx 0.58\textrm{w}_0$ \tabularnewline
    cell(4,1) & $0.5\textrm{w}_0$ \tabularnewline
    cell(5,1) & $0.46\textrm{w}_0$ \tabularnewline
    \hline
\end{tabularx}
}
\label{tab:comparing_table}
\end{table} 
\end{center} 
\end{document}
2

The console output (and the log file) shows

Package array Warning: Column s is already defined on input line 8.

When a column type is already defined, array usually warns and overwrites the definition. But Catch 22 is there waiting for us! The s column type is defined by siunitx, so the warning is shown, but siunitx actually does the necessary definition for the column type at begin document! So you end up anyway with the s column type defined by siunitx and this causes the error, because the input in that column is invalid.

If you aren't sure that overwriting a column type is safe, don't do it.

There are a few other points to note.

  1. Never place table inside center: this has the only effect of producing unwanted vertical space in the document and the table may or may not be in the middle of that vertical space.

  2. Avoid \begin{table}[h] in the hope the float will actually be placed here. It usually won't. LaTeX converts [h] to [ht], but big floats might not find their place in the specified positions and you end up delaying it and all subsequent floats until the end of the current chapter.

  3. Using a group to localize the effect of \renewcommand{\arraystretch}{1.5} is useless, because the former value will be restored anyway after \end{table}.

  4. Put your \label next to the \caption, which it belongs to.

  5. The correct way to go for using “variable X columns” is to use \hsize=<frac>\hsize so that the sum of all fractions is the total number of X columns. In your case if the subdivision is 3/4 for the first column and 1/4 for the second column it should be

    >{\hsize=1.5\hsize}X >{\hsize=0.5\hsize}X
    

Full code, with booktabs features rather than \hline\hline and vertical rules.

\documentclass[12pt,twoside]{report}
\usepackage{siunitx}
\usepackage{comment}
\usepackage{tabularx}
\usepackage{booktabs}

\begin{document}

\begin{table}[htp]
\caption{Comparison between bla bla bla}
\label{tab:comparing_table}

\centering
\renewcommand{\arraystretch}{1.5}

\begin{tabularx}{0.95\linewidth}
 {
  >{\centering\hsize=1.5\hsize}X
  >{\centering\hsize=0.5\hsize}X
 }
\toprule
column 1 title & column 2 title \tabularnewline % [0.5cm]  
\midrule
    cell(1,1) & $\textrm{w}_0$ \tabularnewline
%\begin{comment}
    cell(2,1) & $\frac{\textrm{w}_0}{\sqrt{2}}\approx 0.7\textrm{w}_0$ \tabularnewline
    cell(3,1) & $\frac{\textrm{w}_0}{\sqrt{3}}\approx 0.58\textrm{w}_0$ \tabularnewline
    cell(4,1) & $0.5\textrm{w}_0$ \tabularnewline
    cell(5,1) & $0.46\textrm{w}_0$ \tabularnewline

\bottomrule
%\end{comment}
\end{tabularx}

\end{table} 

\end{document}

enter image description here

Here is the output with

>{\centering}X c

for the column specifications:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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