3

I am trying to centre a table with a lot of content whose width is larger than the text width. The problem is that I am defining the table environment in the preamble so I can reuse it, and this seems to create a lot of issues.

  \NewEnviron{reqtable}{
  \table
  \tabularx{1.3\textwidth}{cX}
  \toprule
  \BODY
  \bottomrule
}[
  \endtabularx
  \endtable
]

I initialise my table with

\begin{reqtable}
item & item \\
\end{reqtable}

The reason I am using NewEnviron from the environ package is so that I can use rules from the booktabs package (see bottomrule not working in a self-made environment)

I have tried the following alterations to center the table.

What else can I try to centre my table?

Note: I am using a twoside layout, so margins are different on odd and even pages. This is why the \adjustwidth command appeals to me most, as it can handle varying margins.


Here is an MWE with tables on different pages wider than the textwidth that I am trying to center.

\documentclass[11pt,a4paper,twoside]{report}

\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{environ}
\usepackage{lipsum}

\NewEnviron{reqtable}{
  \renewcommand{\arraystretch}{1.3}
  \table
  \tabularx{1.3\textwidth}{lX}
  \toprule
  \BODY
  \bottomrule
}[
  \endtabularx
  \endtable
  \renewcommand{\arraystretch}{1}
  \vspace{10pt}
]

\begin{document}

\lipsum[1]

\begin{reqtable}
Some text & \lipsum[1] \\
\end{reqtable}

\lipsum[3]

% This table is on the second page which has different margins
\begin{reqtable}
Other text & \lipsum[1] \\
\end{reqtable}

\end{document}
1

You need to introduce some negative spacing either implicitly or explicitly to pull the table into the left margin

  \NewEnviron{reqtable}{%
  \table\centering
  \hspace*{-.5\textwidth}\tabularx{1.3\textwidth}{cX}%
  \toprule
  \BODY
  \bottomrule
}[%
  \endtabularx\hspace*{-.5\textwidth}%
  \endtable

Note I used the definition form you showed however the use form you showed

\begin{reqtable}{table1}{This is a table}

uses two arguments to the environment (a label and caption?) which are not defined here. ]

Probably works, although untested as you didn't supply an example document. The exact amount of negative space is fairly arbitrary as long as it totals more that .3\textwidth so the whole line is less that textwidth wide and will be centred.

  • This does work, but the issue is I am using a twoside layout, so tables on different pages would need to be pulled either into left or right margin. – Elise Apr 22 '13 at 9:46
  • 1
    @Elise Well the code I put there centres the table so pulls it into both margins, That is anyway the only safe thing to do without writing a lot more code. A floating environment like table is set when encountered but it is not known at that point which page it will appear on so you can not make different layout choice depending on page parity unless you do multiple runs and correct on the second pass by looking at \pageref of this table's label. – David Carlisle Apr 22 '13 at 9:52
1

I don't think you gain by hiding your environment and the markup.

\documentclass{article}
\usepackage{tabularx,booktabs,changepage}

\usepackage[pass,showframe]{geometry} % just to show centering
\usepackage{lipsum} % mock text

% optional argument is the default table placement
% mandatory argument is the fraction of \textwidth for the enlargement
\newenvironment{widetable}[2][htp]
 {\begin{table}[#1]
  \begin{adjustwidth}{-#2\textwidth}{-#2\textwidth}
  \centering}
 {\end{adjustwidth}\end{table}}

\begin{document}

\begin{widetable}{.15}
\begin{tabularx}{\linewidth}{cX}
\toprule
item & \lipsum[2] \\
\bottomrule
\end{tabularx}
\caption{This is a table}\label{table1}
\end{widetable}

\begin{widetable}{.1}
\begin{tabularx}{\linewidth}{cX}
\toprule
item & \lipsum[2] \\
\bottomrule
\end{tabularx}
\caption{This is a table}\label{table2}
\end{widetable}
\end{document}

enter image description here

  • Thank you, but I use this kind of table dozens of times and therefore would prefer the new environment instead of repeating code, and the issue is making \adjustwidth work inside that newenvironment command. – Elise Apr 22 '13 at 10:15

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.