6

I am working on the table

\documentclass{article}
\pagestyle{empty}
\usepackage[landscape]{geometry}
\usepackage{multirow}
\usepackage{booktabs}
\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}
\begin{document}

\begin{table}
  \centering
  \ra{1.5}
  \caption{Caption}
  \begin{tabular}{@{}clllclll@{}}
    \hline 
     File
    &\multicolumn{2}{c}{\textbf{Logistic Regression}} &\phantom{abc}
    &\multicolumn{2}{c}{\textbf{Support Vector Machine}} &\\
    \cline{2-4} \cline{6-8} 
    & sensitivity & specificity & total accuracy && sensitivity 
    & specificity & total accuracy\\ 
   \hline
   $100$ & 0.0790 & 0.1692 & 0.2945 && 0.3670 & 0.7187 & 3.1815 \\
   $101$ & -0.8651& 50.0476& 5.9384&& -9.0714& 297.0923& 46.2143\\
   $102$ & 124.2756& -50.9612& -14.2721&& 128.2265& -630.5455& -381.0930\\
   $103$ & 0.0357& 1.2473& 0.2119&& 0.3593& -0.2755& 2.1764\\
   $104$ & -17.9048& -37.1111& 8.8591&& -30.7381& -9.5952& -3.0000\\
   $105$ & 105.5518& 232.1160& -94.7351&& 100.2497& 141.2778& -259.7326\\
   \hline
 \end{tabular}
\end{table}
\end{document}

And the output is enter image description here I have these problems in formatting the table

  1. (Done) Put a name for the first column
  2. (Done) Center the words for "logistic reg" and "support vector machine"
  3. Output with very small margin like a picture.
  4. Other suggestions making the table looks professional.

  5. How to insert two extra columns to right of first column

  6. (Done)Center "File" with respect to "logistic regression" and 'sensitivity, spe....'

Thanks for your help!

  • Ad 1.: Simply add some words between the first \hline and the &\multicolumn{2}{c}{\textbf{Logistic Regression}}. Ad 2.: centre the words with respect to what? Ad 3.: what does 'like a picture' mean in this context? Ad 4. \usepackage{dcolumn} and align numbers along the decimal point (or use the package siunitx). Also get rid of the \phantom{abc}. Also: why are some numbers math ($100$) and others 'text'. – jon May 28 '13 at 4:49
  • @Jon thanks for your reply. I am not able to upload picture. And the questions might be confusing. Q2: I want to center w.r.t. the 2 sub-columns. Q3: I would like to output the table as picture so that I can include it into my other files. And I need the output has minimum margin. – Lerong May 28 '13 at 5:02
  • Regarding '2.': something can only be centred relative to something else; how must "logistic reg" and "support vector machine" be centred? (that is, which columns are they to be centred over?). Regarding '3.': do you mean put a 'frame' around the entire table? including the caption? Can you provide a link to a table in a book that you like (say, available through Google Books)? – jon May 28 '13 at 5:06
  • It looks like '1.' has been solved (based on my first comment?). – jon May 28 '13 at 5:09
  • @jon YES, thanks. By the way, can I center the 'File' in the first colum? – Lerong May 28 '13 at 5:12
7

1. Put a name for the first column
There is already one, namely "File". Do you want to lower it to the second line? I wouldn't, because it is hierarchically on the same level as "Logistic..." and "Support...".

2. Center the words for "logistic reg" and "support vector machine"
Well, those are already centered, just not on the right columns. In your code, they are each centered on the first 2 columns below the \cline. Change the \multicolumn{2} to \multicolumn{3} and you'll probably get what I believe you mean.

3. Output with very small margin like a picture.
Check the geometry options if you are talking about page geometry, e.g.

\usepackage[landscape,scale=0.95]{geometry}

FYI, geometry enables you to change the geometry of a single page and resetting the usual geometry afterwards.

4. Other suggestions making the table looks professional.
Use the caption package to correct spacing for table captions: while table captions should be placed before the (tabular) content (as you did) and picture captions should be placed after the image, by default LaTeX puts vertical space before the caption label and not after it.

Use \toprule, \midrule, \cmidrule and \bottomrule from the booktabs package (that you are loading without using) instead of \hline and \cline.

Given that you are displaying figures meant to be compared, make the comparison easy to the eye: align your columns containing said figures right, in order to align the decimals. For even better results, use the dcolumn package as in jon's answer to automatically align the figures on their decimal and format them correctly (including using a minus sign instead of a hyphen). FYI, you can use the siunitx package in the rest of your document to format numbers (with \num) and units (with \si), but their use in tabulars is rather tedious. Also, for easy readability, units should go in your tabular's head, not inside the data itself.

Remove the unnecessary @{} and extra middle column; unless you know exactly what and why, (La)TeX does its typesetting pretty well on its own :)

Remove the \arraystretch / \ra "trick".

Given the "File" heading is structurally at the same level as the two "Regression" ones, use the same font for it. Also, put all your headings with a capital (or none, but be coherent).

Finally, to ease your own coding, I would suggest you format the content of your tabular with white spaces to visually separate the columns and resemble your output. I've found that editing tabulars made this way is much more easy, especially when you have to modify documents a few weeks / months after their creation.

Putting it all together

\documentclass{article}
\usepackage[landscape]{geometry}
\usepackage{booktabs,caption,dcolumn}
\newcolumntype{d}[1]{D{.}{.}{4}}% column type for figures with 4 decimals
\newcommand{\subhead}[1]{\multicolumn{1}{c}{#1}}% to format sub-headings of d-type columns
\begin{document}
\begin{table}
  \centering
  \caption{Caption}
  \begin{tabular}{cd{4}d{4}d{4}d{4}d{4}d{4}}
    \toprule 
    \textbf{File}&                       \multicolumn{3}{c}{\textbf{Logistic Regression}}&                    \multicolumn{3}{c}{\textbf{Support Vector Machine}}\\
    \cmidrule(rl){2-4} \cmidrule(rl){5-7} 
                 & \subhead{Sensitivity}& \subhead{Specificity}& \subhead{Total accuracy}& \subhead{Sensitivity}& \subhead{Specificity}& \subhead{Total accuracy}\\ 
    \midrule
         100     &                0.0790&                0.1692&                   0.2945&                0.3670&                0.7187&                    3.1815\\
         101     &             -  0.8651&               50.0476&                   5.9384&             -  9.0714&              297.0923&                   46.2143\\
         102     &              124.2756&             - 50.9612&                 -14.2721&              128.2265&             -630.5455&                 -381.0930\\
         103     &                0.0357&                1.2473&                   0.2119&                0.3593&             -  0.2755&                    2.1764\\
         104     &             - 17.9048&             - 37.1111&                   8.8591&             - 30.7381&             -  9.5952&                 -  3.0000\\
         105     &              105.5518&              232.1160&                 -94.7351&              100.2497&              141.2778&                 -259.7326\\
   \bottomrule
 \end{tabular}
\end{table}
\end{document}

which yields:

Result

  • I think there is also something to be said for making the negative numbers not use a (text) hyphen. – jon May 28 '13 at 5:57
  • Can I center the word "File" with respect to the "logistic regression" and "sensitivity..."? – Lerong May 28 '13 at 6:01
  • @jon Yes, I considered throwing siunitx in there... – Xavier May 28 '13 at 6:13
  • @Lerong I don't get what you mean: "File" is centered above its column, which I guess contains the number of the file. Where else would you want it? – Xavier May 28 '13 at 6:15
  • @Xavier I mean both horizontally and vertically centered. thanks. – Lerong May 28 '13 at 6:21
5

To a large degree, tables are a matter of taste. But I think this is true to the posted image, but a little tider in presentation. Although you loaded booktabs, you aren't using it (but I agree you should); the same goes for multirow (which would serve no purpose in this table, anyway); I don't see what benefit accrues from setting \arraystretch to 1.5; and I still don't understand why you want only some numbers as 'math'. So, as a start:

\documentclass{article}
\pagestyle{empty}
\usepackage[landscape]{geometry}
\usepackage{booktabs}
\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}
\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{.}{4}}
\newcommand{\mlc}[1]{\multicolumn{1}{c}{#1}}

\begin{document}

\begin{table}
  \centering
%  \ra{1.5}% <-- why?
  \caption{Caption}
  \begin{tabular}{@{}c d{4} d{4} d{4} d{4} d{4} d{4} @{}}
    \toprule
    File
    & \multicolumn{3}{c}{\textbf{Logistic Regression}}
    & \multicolumn{3}{c}{\textbf{Support Vector Machine}}\\
    \cmidrule(rl){2-4} \cmidrule(rl){5-7}

    & \mlc{sensitivity} & \mlc{specificity} & \mlc{total accuracy}
    & \mlc{sensitivity} & \mlc{specificity} & \mlc{total accuracy}\\
    \midrule
    $100$ & 0.0790   & 0.1692   & 0.2945   & 0.3670   & 0.7187    & 3.1815 \\
    $101$ & -0.8651  & 50.0476  & 5.9384   & -9.0714  & 297.0923  & 46.2143\\
    $102$ & 124.2756 & -50.9612 & -14.2721 & 128.2265 & -630.5455 & -381.0930\\
    $103$ & 0.0357   & 1.2473   & 0.2119   & 0.3593   &  -0.2755  & 2.1764\\
    $104$ & -17.9048 & -37.1111 & 8.8591   & -30.7381 & -9.5952   & -3.0000\\
    $105$ & 105.5518 & 232.1160 & -94.7351 & 100.2497 & 141.2778  & -259.7326\\
   \bottomrule

  \end{tabular}
\end{table}

\end{document}

enter image description here

  • You made me discover the dcolumn package! I used to define my own commands to format numbers separately and or use siunitx. I'll adapt my answer to reflect this, as it's definitely better. – Xavier May 28 '13 at 6:48
  • @Xavier -- It's a useful, for sure! Your attempt to produce the OP's table looks much better as a result (unfortunately, I already gave a +1 last night....) – jon May 28 '13 at 15:01

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.