0

Prof. wants us to reproduce this table to learn how to LaTeX, but all I'm learning is that LaTeX tables are a nightmare. Been stuck on this for weeks. We were given a lesson on the tabular environment, and told to google things like hhline and multirow. Been trying to follow all the packages' documentation to the letter. Would it be better to just use some external tool and input the table as an image? Is that common in LaTeX? Below are the table I need, my latest attempt at coding it, and how it looks. The desired table

\documentclass{article}

\usepackage{array}
\usepackage{tabularx}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{dcolumn}
\usepackage{hhline}

\newcolumntype{Y}{>{\raggedright\arraybackslash}X}
% Y is left justified column of shifting width
\newcolumntype{Z}{>{\centering\arraybackslash}X}
% Z is center justified column of shifting width
\newcolumntype{W}{>{\raggedleft\arraybackslash}X}
% W is right justified column of shifting width
\newcolumntype{U}{D{+}{\pm}{-1}}
% U is a column with the '+' in each row aligned and turned into \pm
\newcolumntype{.}{D{.}{.}{-1}}
% . is a column aligned on '.'

\begin{document}

\section{Tables in \LaTeX  are Evil}

\begin{table}

\caption{Table 1: This is the table caption, it is placed above the Table itself. The caption should
describe the table fully so that the reader does not need to refer to the text to understand
the table. This table uses many of the alignment and text placement options.}

\begin{tabularx}{600pt}{||Z|Z|Z||Y|W||}

\hhline{|t:===::==:t|}
  Column 1 & Column 2 & Column 3 & Column 4 & Column 5 \\
  (cm)     & (pc)     & ($m^3$)    &          &          \\
\hhline{||-|-|--|-||}
  \multicolumn{1}{||U|}{\multirow{2}{*}{12.3 + 0.3\\98.8 + 0.4}}    & 
  \multicolumn{1}{|U|}{\multirow{2}{*}{154.2 + 0.8\\245.3 + 0.9}}  & 
  \multicolumn{1}{|.||}{\multirow{2}{*}{11.2\\31.8}}                & 
  \multirow{2}{*}{67.8\\134.2}                                   & 
  \multirow{2}{*}{13\\27}                                        \\
% Must leave an empty row to prevent conflicts with the multirows, i think
  \\
\hhline{||-|-|-||~|~||}
  \multicolumn{3}{||Z||}{this text spans 3 columns} & & \\
\hhline{||-|-|--|-||}
  \multirow{4}{=}{this text spans \\4 rows and is \\centered \\vertically}    &
  \multirow{4}{=}{like this}                                                  &
  \multirow{4}{=}{you \\can tweak the \\line breaks \\using stuff}            &
                                                                              &
  \multicolumn{1}{|Z||}{\multirow{4}{=}{and center\\text\\horizontally\\as well}}  \\
 %so those were all 4x multirows, then a linebreak, so i need to leave a gap and a linebreak 3 times?
 \\
 %so the content was row1, the prev comment was row2, this is row3?
 \\
 %gosh i hope this works, latex SUCKS
 \\
\hhline{||=:=:=#=:=||}
  \multicolumn{3}{||Z||}{\multirow{4}{=}{You can even combine the multicolumn \\ and multirow commands}} &  &  \\
  %doing this again i guess
  \\
  %its stupid i have to manually count a bunch of linebreaks to make sure is works
  \\
  %this would be easier in mspaint
  \\
\hhline{|b:=:=:=#=:=:b|}

\end{tabularx}

\end{table}

\end{document}

The table I have, it is bad.

2
  • Welcome to TeX.SE. Do not create a table in another tool and insert the result as an image, it should be a last resort in times of despair. As you are learning, I'd advise using tablegenerator.com, it is a very good, and it will produce the LaTeX syntax. Check here and here for some good-looking tables. Package booktabs is a nice start point.
    – FHZ
    Sep 29, 2022 at 22:01
  • 1
    Your prof is wanting you to use LaTeX to produce the table, not to import a graphic. Sep 30, 2022 at 17:27

2 Answers 2

3
  • Don't use \multirow if you don't have to … and you don't have to here. There is no single actual multirow cell in this table.

    We can put multiline content in a cell by using a tight tabular, here I've used \miniTab.

  • I've replaced one :: in the very first \hhline with t.

  • Don't use tabularx if you don't want the table to stretch to some lengths. A normal tabular is all you need.

  • Don't actual use

    1. double lines,
    2. vertical lines or
    3. vertical double lines

    in the real world.

    Check out the booktabs package and Q&As .

  • The siunitx package (→ ) can help typesetting numbers in tables and with uncertainties (but not both without some manual work, I believe).

Code

\documentclass{article}
\usepackage{multicol}
\usepackage{hhline}

\newcommand*{\ttMacro}[1]{\texttt{\textbackslash#1}}
\newcommand*{\miniTab}[3][c]{\begin{tabular}[#1]{@{}#2@{}}#3\end{tabular}}
\newcommand*{\unc}[2]{\( #1 \pm #2 \)}
\begin{document}

\section{Tables in \LaTeX\ are evil}
\begin{table}
\caption{
  This is the table caption, it is placed above the Table itself.
  The caption should describe the table fully
  so that the reader does not need to refer to the text to understand the table.
  This table uses many of the alignment and text placement options.}
\centering
\begin{tabular}{||c|c|c||l|r||}
  \hhline{|t:===t==:t|}
  Column 1 & Column 2 & Column 3 & Column 4 & Column 5 \\
   (cm)    &   (pc)   & ($m^3$)  &          &          \\
  \hhline{||-|-|--|-||}
  \miniTab{c}{\unc{12.3}{0.3} \\ \unc{98.8}{0.4}}
           & \miniTab{c}{\unc{154.2}{0.8}\\\unc{245.3}{0.9}}
                      & \miniTab{c}{11.2\\31.8}
                                 & \miniTab{l}{67.8\\134.2}
                                            & \miniTab{r}{13\\27} \\
  \hhline{||-|-|-||~|~||}
  \multicolumn{3}{||c||}{this text spans 3 columns} & & \\
  \hhline{||-|-|--|-||}
  \miniTab{l}{this text spans \\4 rows and is \\centered \\vertically}
           & like this
                      & \miniTab{l}{you \\
                                    can tweak the \\
                                    line breaks \\
                                    using \ttMacro{\textbackslash}}
                                & & \miniTab{c}{and center \\
                                                text \\
                                                horizontally \\
                                                as well} \\
  \hhline{||=:=:=#=:=||}
  \multicolumn{3}{||c||}{\miniTab{c}{\\
    You can even combine the \ttMacro{multicolumn} \\
    and the \ttMacro{multirow} commands \\ ~}} & & \\
  \hhline{|b:=:=:=b=:=:b|}
\end{tabular}
\end{table}
\end{document}

Output

enter image description here

1

an alternative:

\documentclass{article}
\usepackage{multicol}
\usepackage{hhline}
\usepackage{tabularx}
\usepackage{ragged2e}

\newcommand*{\ttMacro}[1]{\texttt{\textbackslash#1}}
\newcolumntype{C}{>{\Centering}X}
\renewcommand\tabularxcolumn[1]{m{#1}}
\begin{document}
\noindent
\begin{tabularx}{\linewidth}{||C|C|C||>{\RaggedRight}X|>{\RaggedLeft}X||}\hhline{|t:===t==:t|}
  Column 1 & Column 2 & Column 3 & Column 4 & Column 5\\
   (cm)    &   (pc)   & ($m^3$)  &          &          \\\hhline{||-|-|--|-||}
  $12.3\pm 0.3$\newline$98.8\pm0.4$ &  $154.2\pm0.8$ \newline$245.3\pm0.9$ &  
    11.2\newline31.8 & 67.8\newline134.2 & 13\newline27 \\ \hhline{||-|-|-||~|~||}
  \multicolumn{3}{||c||}{this text spans 3 columns} & & \\\hhline{||-|-|--|-||}
  \RaggedRight this text spans 4 rows and is centered vertically  & like this
    & you can tweak the line breaks using \ttMacro{newline}
    & & \Centering and center text horizontally as well \\\hhline{||=:=:=#=:=||}
  \multicolumn{3}{||p{6cm}||}{You can even combine the \ttMacro{multicolumn}
            and the \ttMacro{multirow} commands} & & \\\hhline{|b:=:=:=b=:=:b|}
\end{tabularx}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .