2

I am new to LaTeX. Started yesterday. I've been working with R and am doing so now but found that it is possible to produce very nice tables with LaTeX.I have been searching and found some code that I have been modifying. The stuff below produces some nice results, however, the "Early" and "Late" names, I would like them to be further left. I now its a stupid question but I keep wrestling for hours with this. And the names "stimuli1, stimuli2 etc. I would like them a few small steps further right. I have tried putting \hspace just before $stimuli1$ to push the whole row further right without result. I have tried \phantom. Wiki page for LaTex commands gives this: \\[*][extra-space] but it does not seem to give the desired results. I could keep going with maybe three or four more commands. Any help with this problem is appreciated. I work in Sweave on R-studio.

\documentclass{article}
\usepackage{booktabs} 
\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}

\begin{document}
\SweaveOpts{concordance=TRUE}
\begin{table*}\centering
\ra{1.3}
\begin{tabular}{@{}rrcrrrcrrr@{}}\toprule
& \multicolumn{3}{c}{$Early$} & \phantom{abc}& &\multicolumn{3}{c}{$Late$} &
\phantom{abc}\\ \cmidrule{0-4} \cmidrule{6-10} 
$stimuli1$ && $stimuli2$ &&& $stimuli1$ && $stimuli2$\\ \midrule 
$CS+$ & $CS-$ & $CS+$ & $ CS+$ && $CS+$ & $CS-$ & $CS+$ & $CS-$\\ \midrule
$Object1$\ \hphantom{xyz}  0.07 (0.15) & 0.16 (0.16) & 0.07 (0.15) & 0.29 (0.45) && 0.36 (0.70) & 0.71 (0.87) & 3.18 (0.15) & 0.07 (0.90) \\
$Object2$\ \hphantom{xyz}  0.07 (0.15) & 0.16 (0.16) & 0.07 (0.15) & 0.29 (0.45) && 0.36 (0.70) & 0.71 (0.87) & 3.18 (0.15) & 0.07 (0.90) \\
\bottomrule
\end{tabular}
\caption{caption}
\end{table*}

\end{document}
0

I have added a macro to help with consistent formating of headers, as well as to reduce the amount of redundant code needed. The macro is called \hf{columns}{content} and takes one argument, the number of columns, just like the multicolumn-environment. It typesets its content in italics, and you can just change one line if you decide that you want to change the format to for example, normal text.

I would advice to split the table, into two tables, as this one gets quite wide. Because it is so wide, I had to set the fontsize to \tiny, which is not the best approach. You could also turn the table around, so that you a long table, instead of a wide one.

In the second piece of code, located at the bottom, I have split the table, by creating a macro which holds all the headers, so they have the same consistent look. I think this would look better than a really wide table.

First output, wide table, smaller font

enter image description here

First code, wide table, smaller font

\documentclass{article}
\usepackage{booktabs}
\usepackage{array}
\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}
\newcommand{\hf}[2]{%HeaderFormat
  \multicolumn{#1}{c}{#2}%
}
\begin{document}
% \SweaveOpts{concordance=TRUE}
\begin{table*}
\centering
\tiny
\ra{1.3}
\setlength{\tabcolsep}{2pt}
\begin{tabular}{%
  @{}>{\itshape}l% Remove space from edge, Set first colun in italice
  *{4}{% 4 columns of the following
    r@{\,}% right column for the mean value, and set the space between the two numbers to be exactly `\,`-space
    >{(}r<{)}% right column for the standard deviation, add paratheses before and after
    }%
  @{}l@{\hspace{2em}}% Need to control the space before and after the column with @{}, so that no `\tabcolsep` is inserted, and then we add the exact space we need.
  *{4}{r@{\,}>{(}r<{)}}% Same as above
  }
\toprule
& \hf{8}{Early} &&\hf{8}{Late}\\

\cmidrule{2-9} \cmidrule{11-18}
& \hf{4}{stimuli1} & \hf{4}{stimuli2} && \hf{4}{stimuli1} & \hf{4}{stimuli2}\\
\midrule
&\hf{2}{CS+} & \hf{2}{CS-} & \hf{2}{CS+} &  \hf{2}{CS+} && \hf{2}{CS+} & \hf{2}{CS-} & \hf{2}{CS+} & \hf{2}{CS-}\\
\midrule
Object1 &0.07&0.15 & 0.16&0.16 & 0.07&0.15 & 0.29&0.45 && 0.36&0.70 & 0.71&0.87 & 3.18&0.15 & 0.07&0.90 \\
Object2  &0.07&0.15 & 0.16&0.16 & 0.07&0.15 & 0.29&0.45 && 0.36&0.70 & 0.71&0.87 & 3.18&0.15 & 0.07&0.90 \\
\bottomrule
\end{tabular}
\caption{caption}
\end{table*}

\end{document}

Second output, split table

enter image description here

Second Code, split table

\documentclass{article}
\usepackage{booktabs}
\usepackage{array}
\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}
\newcommand{\hf}[2]{%HeaderFormat
  \multicolumn{#1}{c}{\itshape#2}%
}
\newcommand{\tableStimuli}[1]{%
\begin{tabular}{%
    @{}>{\itshape}l% Remove space from edge, Set first colun in   italice
    *{4}{% 4 columns of the following
      r@{\,}% right column for the mean value, and set the space between the two numbers to be exactly `\,`-space
      >{(}r<{)}% right column for the standard deviation, add paratheses before and after
      }
  }
\toprule

& \hf{4}{stimuli1} & \hf{4}{stimuli2}\\
 \cmidrule{2-9}% \cmidrule{6-8}
&\hf{2}{CS+} & \hf{2}{CS-} & \hf{2}{CS+} &  \hf{2}{CS+}\\
\midrule
#1\\
\bottomrule
\end{tabular}
}
\begin{document}
\begin{table*}
\centering
\ra{1.3}
\tableStimuli{%
Object1 &0.07&0.15 & 0.16&0.16 & 0.07&0.15 & 0.29&0.45\\
Object2 &0.07&0.15 & 0.16&0.16 & 0.07&0.15 & 0.29&0.45}
\caption{Early}
\end{table*}

\begin{table*}[hbt]
\centering
\ra{1.3}
\tableStimuli{%
Object1 &0.36&0.70 & 0.71&0.87 & 3.18&0.15 & 0.07&0.90\\
Object2 &0.36&0.70 & 0.71&0.87 & 3.18&0.15 & 0.07&0.90}
\caption{Late}
\end{table*}



\end{document}
3
  • Brilliant! I am starting to like LaTeX. And nice that you put in comments! – augusti Sep 14 '16 at 8:14
  • @Granit Just out of curiousity, did you go for the first or the second proposed solution here? – Runar Sep 14 '16 at 14:43
  • Sorry, forgot to say. I went for the first. I found that although it is wide, it works fine. I will have three tables like this, so it would also be inconvenient(?) to split up all three to six tables. Although, in it self, it was a nice solution, and as I will want to produce more tables in the future (and learn LaTeX!), it could definitely be something that I will make use of. I can also say that I still used – augusti Sep 14 '16 at 17:08
2

I propose to use siunitx for the vertical alignment of numbers. Also you should not use math mode to have text in italic: the letterspacing is not that of ordinary words.

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{booktabs, siunitx}
\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}

\begin{document}

%\SweaveOpts{concordance=TRUE}
\begin{table*}\centering\footnotesize\setlength\tabcolsep{3.3pt}
  \ra{1.3}\sisetup{table-format=1.2, table-number-alignment=center, table-space-text-post ={\, (0.00)}}
  \begin{tabular}{@{}>{\itshape}l*{2}{S}c*{2}{S}c*{2}{S}c*{2}{S}@{}}\toprule
    & \multicolumn{5}{>{\itshape}c}{Early} & & \multicolumn{5}{>{\itshape}c}{Late} \\ %
    \cmidrule(r){2-6} \cmidrule(l){8-12}
    & \multicolumn{2}{>{\itshape}c}{stimuli1} & & \multicolumn{2}{>{\itshape}c}{stimuli2} & & \multicolumn{2}{>{\itshape}c}{stimuli1} & & \multicolumn{2}{>{\itshape}c}{stimuli2} \\ \midrule
            & {$ CS+ $} & {$ CS- $} & & {$ CS+ $} & {$ CS+ $} & & {$ CS+ $} & {$ CS- $} & & {$ CS+ $} & {$ CS- $} \\ %
    \midrule
    Object1 &0.07 {\,(0.15)} & 0.16 {\,(0.16)} & & 0.07 {\,(0.15)} & 0.29 {\,(0.45)} && 0.36 {\,(0.70)} & 0.71 {\,(0.87)} & & 3.18 {\,(0.15)} & 0.07 {\,(0.90)} \\
    Object2 &0.07 {\,(0.15)} & 0.16 {\,(0.16)} & & 0.07 {\,(0.15)} & 0.29 {\,(0.45)} && 0.36 {\,(0.70)} & 0.71 {\,(0.87)} & & 3.18 {\,(0.15)} & 0.07 {\,(0.90)} \\
    \bottomrule
  \end{tabular}
  \caption{caption}
\end{table*}

\end{document} 

enter image description here

2
  • I tried this, it was a nice solution! Thanx. I have edited my initial post, instead of a number with four decimals, I have something like this, 0.07(0.06). That is, the first is mean value, and then standard deviation in parentheses. Can the code easily be modified to accommodate this change? – augusti Sep 14 '16 at 6:06
  • Yes. Please see my updated answer. Note that, as the numerical cells are wider, one has to change the font size and the value of \tabcolsep for the table to fit within the margins. Another solution might play with the margins, which you didn't mention in your post, or to play with the 3 parameters. – Bernard Sep 14 '16 at 9:04
1

Adding one column and centering logical groups seems to be that you are expecting:

\documentclass{article}
\usepackage{booktabs} 
\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}

\begin{document}
%\SweaveOpts{concordance=TRUE}
\begin{table*}\centering
\ra{1.3}
\begin{tabular}{@{}rrrcrrrcrrr@{}}\toprule % one r added - PS
& \multicolumn{4}{c}{$Early$} & \phantom{abc} &\multicolumn{4}{c}{$Late$} &
\phantom{abc}\\ \cmidrule{0-4} \cmidrule{6-11} 
%$stimuli1$ && $stimuli2$ &&& $stimuli1$ && $stimuli2$\\ \midrule
&\multicolumn{2}{c}{$stimuli1$} &\multicolumn{2}{c}{$stimuli2$} && \multicolumn{2}{c}{$stimuli1$} & \multicolumn{2}{c}{$stimuli2$}\\ \midrule 
&$CS+$ & $CS-$ & $CS+$ & $ CS+$ && $CS+$ & $CS-$ & $CS+$ & $CS-$\\ \midrule
%$Social$\ \hphantom{xyz}  0.0790 & 0.1692 & 0.0790 & 0.2945 && 0.3670 & 0.7187 & 3.1815 & 0.0790 \\
$Social$& 0.0790 & 0.1692 & 0.0790 & 0.2945 && 0.3670 & 0.7187 & 3.1815 & 0.0790 \\
%$Sphere$\ \hphantom{xyz}  0.0790 & 0.1692 & 0.0790 & 0.2945 && 0.3670 & 0.7187 & 3.1815 & 0.0790\\
$Sphere$ &  0.0790 & 0.1692 & 0.0790 & 0.2945 && 0.3670 & 0.7187 & 3.1815 & 0.0790\\
\bottomrule
\end{tabular}
\caption{caption}
\end{table*}

\end{document}

enter image description here

BTW: Pleas do not use math mode to obtain italicised words. Not $Early$, but \textit{Early} and so on. Probably upright versions of Early and stimuli are much better.

6
  • I've made a change to my original post where I changed the numbers, form 0.xxxx to 0.xx (0.xx). When I then try to compile, I get the error: Issues: 1 badbox. And it seems that the whole table leaves the page. Is there any easy solution for this? – augusti Sep 13 '16 at 18:36
  • Please don't write $Early$ and $Late$. Instead, do write \emph{Early} and \emph{Late}. – Mico Sep 13 '16 at 18:38
  • @Granit It is difficult to say without concrete data. Original area allowed by article is rather small and the horizontal dimension of your tabular is relatively big. Increasing the former or decreasing the latter (e.g., reducing the space after Early and After groups) may help. – Przemysław Scherwentke Sep 13 '16 at 18:43
  • @Mico I know, but because the user is a beginner, I have made only necessary changes, leaving information about improper usage of mathmode in the end part of my answer. – Przemysław Scherwentke Sep 13 '16 at 18:46
  • How can one effectively move the content of a row left or right. Like when I press space the result is this, and also, if I would like to remove some space, i.e. move stuff towards the left. What is the most effective way? – augusti Sep 13 '16 at 19:02

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.