Centering a table wider than the text column - latex

I'm including a table in my LaTeX document and the centering works fine if the table isn't wider than the text column above it, but when the table is wider, the left side of the table sticks to the left side of the text column, and the additional width of the table is on the right side of the page, how can I center the table?

I'd recommend trying the chngpage package.
\documentclass{article}
% allows for temporary adjustment of side margins
\usepackage{chngpage}
% provides filler text
\usepackage{lipsum}
% just makes the table prettier (see \toprule, \bottomrule, etc. commands below)
\usepackage{booktabs}
\begin{document}
\lipsum[1]% just a paragraph of filler text
\medskip% adds some space before the table
\begin{adjustwidth}{-1in}{-1in}% adjust the L and R margins by 1 inch
\begin{tabular}{ll}
\toprule
Sequence & Wide column \\
\midrule
First & Vestibulum porta ultricies felis. In nec mi. \\
Second & Nam vestibulum auctor nibh. In eleifend,
lacus id tristique ullamcorper, mauris urna convallis elit. \\
Third & Ut luctus nisi quam lobortis magna. Aenean sit amet odio
et sapien rutrum lobortis. \\
Fourth & Integer dictum accumsan purus. Nullam erat ligula,
dictum sed, feugiat nec, faucibus id, ipsum. \\
\bottomrule
\end{tabular}
\end{adjustwidth}
\medskip% adds some space after the table
\noindent\lipsum[2]% just a paragraph of filler text
\end{document}
The documentation for the chngpage package is located at the bottom of the chngpage.sty file. I've pulled out the docs for the adjustwidth environment:
Within an adjustwidth environment
the left and right margins can be
adjusted. The environment takes one
optional argument and two required
length arguments:
\begin{adjustwidth}[]{leftmargin}{rightmargin}
A positive length value will increase the relevant margin
(shortening the text lines) while a
negative length value will decrease
the margin (lengthening text lines).
An empty length argument means no
change to the margin. At the end of
the environment the margins revert to
their original values.
For example, to extend the text
into the right margin:
\begin{adjustwidth}{}{-8em}
Any appearance of the optional
argument (even just []) will cause the
values of the margins to switch
between odd and even pages.
If the document is being set
twosided it might be advantageous to
have any wider text extending into the
outside margin. This could be done via
the optional argument, as:
\begin{adjustwidth}[]{}{-8em}
To have the adjusted text
horizontally centered with respect to
any surrounding text, the margins
should be adjusted equally:
\begin{adjustwidth}{-4em}{-4em}

Latex: Centering table larger than textwidth
Usually, you can center tables with \center. But when the table is longer than the \textwidth, it will be align with the left side margin. You can temporarily adjust the textwidth.
% allows for temporary adjustment of side margins
\usepackage{chngpage}
\begin{table}
\begin{adjustwidth}{-.5in}{-.5in}
\begin{center}
\begin{tabular}{|c|}
\hline
And here comes a very long line. And here comes a very long line. And here comes a very long line. \\
\hline
\end{tabular}
\caption{This Table is longer than the text width. And its caption is really long, too. This Table is longer than the text width. And its caption is really long, too. This Table is longer than the text width. And its caption is really long, too. This Table is longer than the text width. }
\label{myTable}
\end{center}
\end{adjustwidth}
\end{table}

If you're using a \table float, the \begin{adjustwidth} ... \end{adjustwidth} has to be contained inside it.

In figures, the figure environment must contain the adjustwidth env.. Furthermore, caption should be left outside of this environment to align with the overall figure's width:
\begin{figure}[h]
\begin{adjustwidth}{-1in}{-1in}% adjust the L and R margins by 1 inch
\centering
\includegraphics[scale=0.44]{res/sth.png}
\end{adjustwidth}
\caption{sth}
\label{fig:sth}
\end{figure}

Are you using a multi-column document? I so, consider the table* variant environment.
In a single column environment your options run to:
Increase the textwidth. But the default margin were choosen for good ergonomic reasons, so this is to be discouraged beyond a minimal tweaking.
Reduce the text size in the table (i.e. \small or even \footnotesize inside the tabular environment). Again, this is less than optimal.
Use the rotating package as suggested in the link Stephan202 gave. I used this for a couple of very large tables in my dissertation (with only the p positioning options) and it came out very nicely.

Related

Latex table: Vertically center text in one column, other columns top aligned with fixed width

I try to vertically center a text in the first column of a latex table with unknown length of the text of other columns. The other columns should be top aligned.
I already tried with tabular, tabularx, tabu table environment. All the approaches I have found in the internet for vertically center something are using the baseline or some kind of multirow environment.
multirow: not working, because of the unknown number of rows generating a long text in a fixed width column.
baseline: not working, because all other columns should be top aligned.
\documentclass{article}
\begin{document}
\begin{tabular} {| p{2cm} p{2cm} p{2cm} |}
\hline
centered & This is a long top aligned text, dynamically length. & This is a long top aligned text, much longer than the previous one...or shorter. Who knows what text length is given to me in my new environment. \\
\hline
\end{tabular}
\end{document}
I want that the text "centered" is vertically centered in this row.
A nearly identical question has several answers in TexStackExchange.
Also: https://www.latex-tables.com/ suggests using vcell for this kind of thing
\documentclass{article}
\usepackage{vcell}
\begin{document}
\begin{table}
\centering
\begin{tabular}{| p{2cm} p{2cm} p{2cm} |}
\hline
\vcell{centered}
& \vcell{This is a long top aligned text, dynamically length.}
& \vcell{This is a long top aligned text, much longer than the previous one...or shorter. Who knows what text length is given to me in my new environment.} \\
[-\rowheight]
\printcellmiddle
& \printcelltop
& \printcelltop \\
\hline
\end{tabular}
\end{table}
\end{document}
Meanwhile I came around with a workaround. I call it workaround because I think there should exist a simpler solution to this "easy" problem.
\documentclass{article}
\usepackage{adjustbox}
\usepackage{calc}
\usepackage{makecell}
\begin{document}
\newdimen\widthcola
\setlength{\widthcola}{2cm}
\newdimen\widthcolb
\setlength{\widthcolb}{2cm}
\newdimen\widthcolc
\setlength{\widthcolc}{2cm}
\newcommand{\tabline}[3]{
\newdimen\heightcella
\setlength{\heightcella}{\totalheightof{\makecell[t{p{\widthcola}}]{#1}}}
\newdimen\heightcellb
\setlength{\heightcellb}{\totalheightof{\makecell[t{p{\widthcolb}}]{#2}}}
\newdimen\heightcellc
\setlength{\heightcellc}{\totalheightof{\makecell[t{p{\widthcolc}}]{#3}}}
\newdimen\heightcellmax
\setlength{\heightcellmax}{\heightcella}
\setlength{\heightcellmax}{\maxof{\heightcellmax}{\heightcellb}}
\setlength{\heightcellmax}{\maxof{\heightcellmax}{\heightcellc}}
\adjustbox{padding=0mm, margin=0mm, raise=-0.5\heightcellmax+0.5\heightcella}{\makecell[t{p{\widthcolc}}]{#1}} & \makecell[t{p{\widthcolc}}]{#2} & \makecell[t{p{\widthcolc}}]{#3} \\
}
\begin{tabular} {| p{\widthcola} p{\widthcolb} p{\widthcolc} |}
\hline
\tabline{centered}{This is a long top aligned text, dynamically length.}{This is a long top aligned text, much longer than the previous one...or shorter. Who knows what text length is given to me in my new environemnt.}
\hline
\end{tabular}
\end{document}
So, does anybody know a simpler and more straightforward solution?
With the tabularray package it is easy to set the alignment for each column separately:
\documentclass{article}
\usepackage{tabularray}
\begin{document}
\begin{tblr}{| Q[2cm,valign=m] Q[2cm,valign=h] Q[2cm,valign=h] |}
\hline
centered & This is a long top aligned text, dynamically length. & This is a long top aligned text, much longer than the previous one...or shorter. Who knows what text length is given to me in my new environment. \\
\hline
\end{tblr}
\end{document}

In TeX, how do I define the label style for compactenum globally?

I have a couple of compactenum elements in my document. I would like define color and type for all labels in a more elegant way than this:
\begin{compactenum}[label=\textcolor{black}{\arabic*.}]
\item lorem
\item ipsum
\item dolor
\end{compactenum}
\begin{compactenum}[label=\textcolor{black}{\arabic*.}]
\item sit
\item amet
\end{compactenum}
How do I set it globally the most elegant way? Is there a way where I can define the behavior for compactenum without using [...] at all?
What you need is \setdefaultenum{\color{black} 1.}{}{}{}. It will set your outer label to be black arabic label. For more information, refer to paralist documentation Here's a MCVE:
\documentclass{article}
\usepackage{paralist,xcolor}
\setdefaultenum{\color{black} 1.}{}{}{}% What you need!
% The 1. is the equivalent of \arabic*.
% The other {}{}{} refer to the list levels,
% i.e. {second level}{third level}{fourth level}
\begin{document}
\begin{compactenum}
\item lorem
\item ipsum
\item dolor
\end{compactenum}
\end{document}

Text exceed from cover page, how to solve it?

I have brought the title, which is a bit long sentence in cover page.
However, it exceeds the page width, how to correct it? Thanks
\vspace{30mm} \centerline{{\bf Title BLAH Blah}}
You could try using the center environment, as it allows natural wrapping at within the text block:
\begin{center}
\bfseries
Lorem ipsum dolor sit amet, consectetur adipiscing elit \ldots
\end{center}
Note the use of \bfseries rather than \bf:
Does it matter if I use \textit or \it, \bfseries or \bf, etc
Will two-letter font style commands (\bf , \it , …) ever be resurrected in LaTeX?

align-like environment in LaTeX for lines of texts

I am looking for a LaTeX environment, that is capable of formatting given paragraph(s) with a slight indentation and increasing line counters within parenthesis, e.g.:
(1) Lorem Ipsum
(2) Dolor Sit
These numbers should continue to increase throughout the document, i.e. there should be no number used twice. It would behave almost like the align environment- including the possibility to \ref to individual lines, except that I do not wish to use it for math but rather for text snippets.
Unfortunately, I am not that LaTeX savvy, so I cannot really define such an environment myself.
Is this what you're looking for?
\documentclass{article}
\usepackage{lipsum} % just for blind text
\usepackage{enumitem}
\newlist{masterlist}{enumerate}{1}
\setlist[masterlist]{label=(\arabic*),resume}
\begin{document}
\lipsum[1]
\begin{masterlist}
\item foo
\item bar
\end{masterlist}
\lipsum[2-3]
\begin{masterlist}
\item baz
\item boo
\end{masterlist}
\end{document}
In fact you want an enumerate list with number in a non default presentation. An easy way for this is the enumerate package:
\documentclass{article}
\usepackage{enumerate}
\begin{document}
\begin{enumerate}[(1)]
\item test1
\item test2
\end{enumerate}
\end{document}
You could use the align environment (or other math-based environments) and typeset your text using \text{...}. Example:
\begin{equation}\label{mylabel}
\text{Numbered like an equation, typeset like text.}
\end{equation}
Beware, though, that this is only suitable for short snippets of text, since you are, after all, inside a math environment, so there's no automatic line wrapping.

LaTeX/Beamer, column environment. Horizontal alignment of overlays

I am trying to prepare a presentation using beamer. I want to have two columns that walkthrough some algebraic manipulations. On the left an explanation of the steps taken, on the right the results.
\documentclass{beamer}
\begin{document}
\begin{frame}[t]
Not in a column
\begin{columns}[t]
\begin{column}{0.5\textwidth}
\only<2->{Some text}
\only<3->{
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
}
\end{column}
\begin{column}{0.5\textwidth}
\only<2->
{
\begin{equation}
E = mc^2
\end{equation}
}
\only<3->
{
\begin{equation}
F = ma
\end{equation}
}
\end{column}
\end{columns}
\end{frame}
\end{document}
And here's some LaTeX which does that (with junk words and equations). When this is compiled the text and maths are not aligned with each other. I wouldn't really expect them to be either as LaTeX will position the text in each column individually, not caring about the other frames.
Does anyone have any ideas on how to achieve the result I am after. I'm not committed to the columns at all, but I am committed to the equation numbers.
The preferred way to get aligned equations with numbering is amsmath package's align environment. See its documentation for help with that. It's quite simple, something like:
\begin{align}
f(x) & = \cos^2 x \\
g(x) & = \sin^2 x
\end{align}
There are a lot of variations trying to cover most conceivable equation alignment needs (again, check out the documentation).
As for your two-column proof format, I'm not as sure about the best way. A quick and dirty way would be to add it as a second column within the environment, something like:
\begin{align}
f(x) & = \cos^2 x & \text{this is the first function} \\
g(x) & = \sin^2 x & \text{this is the second function}
\end{align}
but this is no good for multi-line explanation, and puts the numbering to the right of the text. I'll try and think of a way (one that doesn't involve a lot of custom-defined environments, since surely someone's done this before).
Edit: As a starting point, this [sort of] works:
You can't do any alignment within the align environment (the & confuses things), and there are some vertical alignment issues - the align environment pads itself above and below, and the text in the right-hand cell. Maybe it's heading in a good direction, though!
\begin{tabular}{p{3 in}|l}
\begin{align} f(x) = \sin^2 x \end{align} &
this is the first equation \\
\begin{align} g(x) = \cos^2 x \end{align} &
this is the second equation
\end{tabular}
Usually you'd use amsmath's align or align* environment, but unfortunately it doesn't play nicely with beamer (for fundamental reasons that nobody wants to fix).
The Beamer user guide has a section on this at page 106 that does exactly what you've done. Apparently there's a workaround described in that document too.

Resources