1

Problem statement

I am using a custom-written document class that makes excessive use of a relatively wide outer margin. To strenghten the eye's judgement for what constitutes the main text block vs. the margin, I would like to automatically typeset all tables at one of two widths:

  • If the tabular data requires less space than the main text block, the table width should be identical to the width of the main text block, i.e., \textwidth.
  • Or, if the tabular data requires more space than the main text block, the table width should be identical to the width of the main text block plus the margin plus any space between the two, i.e., \textwidth + \marginparwidth + \marginparsep

What I imagine an according custom environment to look like

The idea is to write a new LaTeX environment (called autotable below), which I pass the tabular data to, and which will then "evenly distribute" (see below) that data across the automatically chosen table width.

\begin{autotable}% Imaginary macro that automatically determines applicable table width
  \begin{tabular}{lll}% Or some alternative command from another package?
    Foo & Bla & Moo & Ma\\
    Ma  & Bla & Foo & Ha
  \end{tabular}
\end{autotable}

I am looking for implementations of such a custom LaTeX environment, which may employ extra packages such as tabularx if required. The fewer arguments it requires (if any), the better.

Problems that such a custom environment must solve

  • One question that arises is what "evenly distribute" exactly means in this context. This answer to a related question seems to provide a good solution in this regard, because it visually emphasizes the left and right border of the main text block, which is what I'm after.
  • The other issue is how to determine whether the tabular data "comfortably fits" into the main text block, or whether use of the margin is required. Here I would be grateful for pointers.
  • Well, I think tabularx is the first choice – user31729 Jul 12 '16 at 10:25
1

Expanding on David's approach at the cited question (Automatically stretch table to evenly fill horizontal space?).

The syntax \autotable{columns}content\endautotable places the content into a temporary tabular box using the specified columns (see ADDENDUM for environment form of solution). If it is undersized, it uses David's approach with the dimension set to \textwidth. If oversized, it uses David's approach with the size set larger.

I didn't pre- and post-pend the macro with \pars, but that might be advisable, since it would seem the OP would never combine it on a line with other text or content.

\documentclass{article}
\usepackage{tabularx}
\usepackage{showframe,lipsum}
\def\autotable#1#2\endautotable{\noindent%
  \setbox0=\hbox{\begin{tabular}{#1}#2\end{tabular}}%
  \ifdim\wd0>\textwidth\relax%
    \begin{tabular*}{\dimexpr\textwidth+\marginparwidth+\marginparsep\relax}%
      {@{\extracolsep{\stretch{1}}}#1@{}}#2\end{tabular*}%
  \else%
    \begin{tabular*}{\textwidth}{@{\extracolsep{\stretch{1}}}#1@{}}#2\end{tabular*}%
  \fi%
}
\parskip 1ex
\begin{document}

\lipsum[1]

\autotable{llll}
    Foo & Bla & Moo & Ma\\
    Ma  & Bla & Foo & Ha
\endautotable

\lipsum[2]

\autotable{llll}
    Foo Foo Foo Foo Foo Foo Foo & Bla Bla Bla Bla & Moo Moo Moo Moo & Ma\\
    Ma  & Bla & Foo & Ha
\endautotable

\lipsum[3]
\end{document}

enter image description here


ADDENDUM

The OP has asked for the solution in environment form. Here it is, using the environ package.

\documentclass{article}
\usepackage{tabularx,environ}
\usepackage{showframe,lipsum}
\NewEnviron{autotable}[1]{\noindent%
  \setbox0=\hbox{\begin{tabular}{#1}\BODY\end{tabular}}%
  \ifdim\wd0>\textwidth\relax%
    \begin{tabular*}{\dimexpr\textwidth+\marginparwidth+\marginparsep\relax}%
      {@{\extracolsep{\stretch{1}}}#1@{}}\BODY\end{tabular*}%
  \else%
    \begin{tabular*}{\textwidth}{@{\extracolsep{\stretch{1}}}#1@{}}\BODY\end{tabular*}%
  \fi%
}
\parskip 1ex
\begin{document}

\lipsum[1]

\begin{autotable}{llll}
    Foo & Bla & Moo & Ma\\
    Ma  & Bla & Foo & Ha
\end{autotable}

\lipsum[2]

\begin{autotable}{llll}
    Foo Foo Foo Foo Foo Foo Foo & Bla Bla Bla Bla & Moo Moo Moo Moo & Ma\\
    Ma  & Bla & Foo & Ha
\end{autotable}

\lipsum[3]
\end{document}
|improve this answer|||||
  • Would there be a way to encapsulate the \autotable ... \endautotable instead into a \begin{autotable} ... \end{autotable} via \newenvironment or the xparse package's \DeclareDocumentEnvironment? – Florian H. Jul 12 '16 at 11:42
  • @FlorianH. Please see ADDENDUM to my answer. – Steven B. Segletes Jul 12 '16 at 11:52
  • Ah, you beat me to it by 2 minutes :) For the record, I got the inspiration from this answer to a different question. Thanks, this is a great answer! – Florian H. Jul 12 '16 at 12:12

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.