调整表格宽度使其比文本列更宽

51

我在我的LaTeX文档中包含了一个表格,如果该表格的宽度不超过上面的文本列,则居中效果良好,但是当表格宽度超过文本列时,表格的左侧将粘附在文本列的左侧,而表格的额外宽度在页面的右侧。如何将表格居中?

5个回答

41
我建议尝试使用chngpage包。
\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}

chngpage包的文档位于chngpage.sty文件底部。我摘出了adjustwidth环境的文档:

adjustwidth环境中,可以调整左右边距。该环境有一个可选参数和两个必需的长度参数:

\begin{adjustwidth}[]{leftmargin}{rightmargin}

A positive length value will increase the relevant margin

在调整页边距时,正长度参数将增加 边距(缩短文本行),而负长度参数则会减小 边距(延长文本行)。如果长度参数为空,则不改变边距。在环境结束时,边距会恢复到其原始值。

例如,将文本向右边距扩展:

\begin{adjustwidth}{}{-8em}

任何可选参数的出现(即使只是[])都会导致边距值在奇数页和偶数页之间切换。

如果文档是双面设置,则将任何更宽的文本延伸到外部边距可能是有优势的。这可以通过可选参数实现,例如:

\begin{adjustwidth}[]{}{-8em}

为了使调整后的文本在水平方向上相对于周围的文本居中,应该同等地调整边距:

\begin{adjustwidth}{-4em}{-4em}


2
很好的回答。有一个注意事项:最好使用更新的“changepage”包,它几乎相同但使用与memoir类相同的接口。所有都是由同一作者编写的。 - Will Robertson
威尔,说得对。但是由于“changepage”包非常新,我在这里使用了“chngpage”包,因为它随所有LaTeX发行版一起提供。对于Zequj来说,“adjustwidth”环境的语法在“chngpage”和“changepage”包之间略有不同。 - godbyk
有没有办法让它与tabularx环境一起工作? - RegressForward

24

Latex:如何让比文字宽度更宽的表格居中

通常,可以使用\center指令使表格水平居中。但是当表格比\textwidth(即文字宽度)更长时,表格会与页面左边缘对齐。你可以临时调整\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}

2
在Stack Overflow上,包含对一个有良好文档来源的外部引用是受欢迎的,但重要的是在你的回答中包含最重要/相关的片段,并提供如何回答问题的上下文,因为如果该链接失效(服务器崩溃等),那么你的回答就变得无用了。 - mbinette

17
如果您使用表格浮动,则 \begin{adjustwidth} ... \end{adjustwidth} 必须包含在其中。

5
在图形方面,figure环境必须包含adjustwidth环境。此外,标题应该放在这个环境之外,以便与整个图形的宽度对齐。
\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}

1

您是否正在使用多列文档?如果是,可以考虑使用table*变体环境。

在单列环境中,您的选项包括:

  • 增加textwidth。但默认边距是出于良好的人体工程学原因而选择的,因此除了最小的微调外,不建议这样做。
  • 在表格中减小文本大小(即在tabular环境内使用\small或甚至\footnotesize)。同样,这不是最佳选择。
  • rotating package所建议的那样使用Stephan202给出的链接。我在我的论文中使用了这个方法来处理一些非常大的表格(只使用了p定位选项),效果非常好。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接