3

I am having a weird issue.

I am trying to create a table that requires multiline headers centered both horizontally and vertically. I figured out how to do this using

{\centering\arraybackslash}m{<fraction>\textwidth}

for each column header and does exactly what I want. However, even when i specify the fractions such that they all add to 0.85ish, the table extends past the main body of text into the margin between the body of text and margin notes. I have attached a bit of sample

\documentclass[12pt,a4paper,twoside]{report}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\author{Matthew Mouawad}

\usepackage[showframe]{geometry}
\usepackage{array}


\begin{document}

    \begingroup

    \renewcommand{\arraystretch}{1.5}
    \begin{table}[ht]
        \caption[Brachytherapy studies]{A list of studies reporting cosmetic outcome as well as with a focus on studies from the last 10 years} 
        \label{tab-brachy-review}
        \footnotesize
        \centering
        \begin{tabular}{>{\centering\arraybackslash}m{0.28\textwidth}>{\centering\arraybackslash}m{0.07\textwidth}>{\centering\arraybackslash}m{0.15\textwidth}>{\centering\arraybackslash}m{0.13\textwidth}>{\centering\arraybackslash}m{0.14\textwidth}>{\centering\arraybackslash}m{0.085\textwidth}}       %{cccccc}
            \hline
            test & test & test & test & test & test \\
            1    & 2    & 3    & 4    & 5    & 6    \\
            1    & 2    & 3    & 4    & 5    & 6    \\
            1    & 2    & 3    & 4    & 5    & 6\\ \hline
        \end{tabular}
        \normalsize
    \end{table}
    \endgroup

\end{document}

Latex table margin issue

2
  • Welcome to TeX.SE.
    – Mico
    Commented Mar 2, 2019 at 17:10
  • Off-topic: If you place the instruction \renewcommand{\arraystretch}{1.5} immediately after \begin{table}[ht], you could dispense with the \begingroup and \aftergroup directives. Separately, there's no need for the \normalsize directives since the scope of the earlier \footnotesize directive ends with \end{table}.
    – Mico
    Commented Mar 2, 2019 at 17:30

1 Answer 1

3

You have specified six usable column widths, and those add up to 0.855\textwidth. However, to obtain the full width of a column, one has to add 2\tabcolsep to the usable width. The default value of the \tabcolsep parameter is 6pt in most document classes I'm familiar with. What you've "discovered", then, is that 0.855\textwidth+12\tabcolsep>\textwidth -- at least for the current values of \textwidth and \tabcolsep.

To make the tabular material fit inside the width of the text block, one could go about calculating, by hand, what the value of \tabcolsep would have to be in order to ensure that 12\tabcolsep=0.145\textwidth. (Hint: 0.145/12=0.1208333.) However, one could also go about the job much more simply:

  • use a tabular* environment instead of a tabular environment,

  • set the desired width of the tabular* environment to \textwidth,

  • set \tabcolsep to 0pt, and

  • use the @{\extracolsep{\fill}} directive inside the second argument of the tabular* environment to make LaTeX figure out the permissible amount of intercolumn whitespace.

enter image description here

Aside: The vertical lines in the preceding screenshot denote the edges of the text block. These lines are inserted because the geometry package is being loaded with the option showframe.

\documentclass[12pt,a4paper,twoside]{report}
%\usepackage[latin1]{inputenc} % shouldn't you be using 'utf8'?
\usepackage{amsmath}
%%%\usepackage{amsfonts} % is loaded automatically by 'amssymb'
\usepackage{amssymb}
\usepackage{graphicx}

\usepackage[showframe]{geometry}

\usepackage{array}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1\textwidth}}

\begin{document}
\begin{table}[ht]
\renewcommand{\arraystretch}{1.5}
\setlength\tabcolsep{0pt}
\caption[Brachytherapy studies]{A list of studies reporting cosmetic 
 outcome as well as with a focus on studies from the last 10 years} 
\label{tab-brachy-review}
\footnotesize
%%%\centering %% not needed
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}%
      M{0.28}M{0.07}M{0.15}M{0.13}M{0.14}M{0.085}}  %{cccccc}
            \hline
            test & test & test & test & test & test \\
            1    & 2    & 3    & 4    & 5    & 6    \\
            1    & 2    & 3    & 4    & 5    & 6    \\
            1    & 2    & 3    & 4    & 5    & 6    \\ 
            \hline
\end{tabular*}
\end{table}
\end{document}
4
  • Thank you very much. I have marked as solved as this fixed the problem. I am very new to latex so learning on the fly. I do not understand the last point with the @{\extracolspe{\fill}} - I understand what you said it does but I do not understand the syntax. If possible, do you think you could elaborate a tiny bit? I am not even sure what to search to understand this. Thanks!
    – thatguy14
    Commented Mar 2, 2019 at 17:44
  • @thatguy14 - The admittedly somewhat intimidating-looking expression @{\extracolsep{\fill}} instructs LaTeX to "fill" the five inter-column spaces with enough whitespace so that the overall width of the tabular* environment equals 1\textwidth. (We already know that the usable widths of the six columns sum to 0.855\textwidth; LaTeX is capable of figuring out how much "fill" to insert so that the whitespace inserted in the five intercolumn spaces comes to 0.145\textwidth.)
    – Mico
    Commented Mar 2, 2019 at 17:48
  • 1
    What does the @ syntax mean? I've seen it used in different contexts but cannot find what exactly it is supposed to represent or do? Thanks, sorry if these are silly questions.
    – thatguy14
    Commented Mar 2, 2019 at 18:02
  • @thatguy14 - In a LaTeX document, the @ ("at") symbol can have various meanings or uses; it very much depends on the context. In tabular-like environments, an @{...} construct informs LaTeX what to insert (if anything) between columns. For instance, @{\extracolsep{\fill}} means, loosely translated, "insert \fill [a TeX parameter] of whitespace in each intercolumn space". A separate example: in tabular-like environments, one often encounters @{} at the start or end of the column spec list; @{} means, "don't insert anything here".
    – Mico
    Commented Mar 2, 2019 at 18:15

You must log in to answer this question.

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