2

Much like this question, I want to force a table to fit within the vertical page margins.

Any ideas?

Edit:

Margin size: \usepackage[left=3cm,right=3cm,textheight=23.5cm]{geometry}

Present situation: Output and LyX text.

  • Put it in a box and scale it to the text height. It would help us help you if you gave us a minimum working example to start with. – Steven B. Segletes Jan 31 '14 at 14:09
  • I have edited my question to elaborate on my problem. By "box", do you mean float? @StevenB.Segletes – SplitteMineBramsejl Jan 31 '14 at 14:32
  • 2
    By box, I mean something like \newsavebox\mybox\sbox{\mybox}{your table goes here}\scalebox{scale factor}{\usebox{\mybox}}, where \scalebox is part of the graphicx package. scale factor is a number. The picture helps, but the code you used to get there would be more helpful, so that those who are willing to help you don't have to retype it all from scratch. – Steven B. Segletes Jan 31 '14 at 14:37
  • Sorry for the lack of code. I am using LyX/LaTeX at a rookie level and I just follow the classicthesis template. I thought there would be a similar one-liner like \noindent\resizebox{\textwidth}{!}{ table here } to make the table fit within the vertical margins. Anyway, your answer seem to work, thank you, but you have written it as a comment. – SplitteMineBramsejl Jan 31 '14 at 14:49
  • 1
    If you would like, I will turn it into an answer, using typical tabular material. – Steven B. Segletes Jan 31 '14 at 14:51
  • Please do, and I will accept it.

    It just frustrates me that I can't get the geometry package to work my way. From the LyX wiki it is described how to fit the table horizontally by keeping the aspect ratio of your table, using ! as the height parameter: \resizebox{\textwidth}{!}{argument}.

    – SplitteMineBramsejl Jan 31 '14 at 15:06
  • One last point: if your table is exceeding the vertical limit of \textheight, but is within the horizontal extent of \textwidth, then the \resizebox macro you cite will not shrink the table, since it is already less wide than \textwidth. – Steven B. Segletes Jan 31 '14 at 15:14
  • That is my exact situation and problem. Do you know if any command similar to \resizebox within geometry is able of resizing tables vertically? I.e. tables breaking the vertical but not hortizontal extent of \textwidth. – SplitteMineBramsejl Jan 31 '14 at 15:21
  • 1
    Well, it won't work with a float, but if it is just the tabular you need resized, then my scalerel package has a macro \scaleto{tabular material goes here}{\textheight} – Steven B. Segletes Jan 31 '14 at 15:34
  • You are a goldmine. Thanks again for the help. – SplitteMineBramsejl Jan 31 '14 at 16:04

1 Answers1

2

In my comment, I advocated something like:

\newsavebox\mybox
\sbox{\mybox}{your table goes here}
\scalebox{scale factor}{\usebox{\mybox}}

where scalefactor is a number and your table is something like a tabular. In this illustrative example below, I first set the text height shorter (2.8") just so it is easier to see what is going on. The first thing I do I set a large tabular without any changes. Because it exceeds the length of page 1, it ends up on page 2, and you see that it stretches down to the page number.

So, after clearing a page, I show how placing the table into a box and scaling it to a size smaller than unity can resolve the height issue (page 3). Of course, the font size is no longer uniform through the document.

On page 4, I show how this approach can be incorporated into a float, such as a table.

\documentclass[12pt]{article}
\usepackage{graphicx}
\textheight 2.8in
\begin{document}
{\centering
\begin{tabular}{|l|l|}
\hline
Variable & description of the variable\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
\end{tabular}\par
}
\clearpage
\newsavebox\mybox
\sbox{\mybox}{%
\begin{tabular}{|l|l|}
\hline
Variable & description of the variable\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
\end{tabular}
}
{\centering\scalebox{.9}{\usebox{\mybox}}\par}
\clearpage
\begin{table}
\caption{My caption}
{\centering\scalebox{.85}{\usebox{\mybox}}\par}
\end{table}
\end{document}

enter image description here

If the table to be scaled is not a float, but merely a tabular, it can be scaled exactly to the \textheight using the \scaleto{object}{height} feature of the scalerel package:

\documentclass[12pt]{article}
\textheight 6in
\usepackage{scalerel}
\begin{document}
\newsavebox\mybox
\sbox{\mybox}{%
\begin{tabular}{|l|l|}
\hline
Variable & description of the variable\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
Variable & description\\
\hline
\end{tabular}
}
{\centering\scaleto{\usebox{\mybox}}{\textheight}\par}

\end{document}