惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Blog — PlanetScale
Blog — PlanetScale
小众软件
小众软件
F
Fortinet All Blogs
博客园 - 叶小钗
博客园_首页
D
DataBreaches.Net
Apple Machine Learning Research
Apple Machine Learning Research
U
Unit 42
爱范儿
爱范儿
aimingoo的专栏
aimingoo的专栏
博客园 - Franky
Martin Fowler
Martin Fowler
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
A
About on SuperTechFans
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
IT之家
IT之家
M
MIT News - Artificial intelligence
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN Mintty Tips and Configurations
Change Table Column Width in LaTeX
2020-03-08 · via jdhao's digital space

When we create tables in LaTeX, the table column width will be automatically decided based on the content in the table cell. Sometimes, maybe we are not satisfied with the default column width and want to customize the column width. In this post, we will explain how to it in LaTeX.

Our initial table is like the following:

The code to produce this table is:

Click to show the code.
\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}

\begin{document}

\begin{table}[t]
    \centering
    \begin{tabular}{llll}
        \toprule
        \multirow{2}{*}{Country} & \multicolumn{2}{c}{GDP} & Change \\
        \cmidrule{2-3} \\
        {} & 2010 & 2015 & {} \\
        \midrule
        United States & 14964.400 & 18036.650 & 20.53\% \\
        China & 5812.464 & 11226.186 & 93.14\% \\
        Japan & 5793.455 & 4382.420 & -24.36\% \\
        Germany & 3309.668 & 3365.293 & 1.68\% \\
        United Kingdom & 2246.079 & 2863.304 & 27.48\% \\
        \bottomrule
    \end{tabular}
\end{table}

\end{document}

Column type with width parameter#

Column width of the produced table is small. To control the column width, we need to use another three column types p, m, b:

Column typeDescription
p{width}Create fixed width column with top alignment
m{width}Create fixed width column with middle alignment
b{width}Create fixed width column with bottom alignment

The width parameter can be absolute value, like 2cm, 20pt etc. It can also be a relative value, e.g., 0.2\textwidth. Using relative width is preferred since the table width are often measured relative to \textwidth.

Table cell horizontal alignment#

The above three column types only specify the vertical alignment of table column cell. To specify the horizontal alignment of table cell, we need to use the following three command provided by the array package:

AlignmentDescription
\centeringCenter alignment
\raggedrightLeft alignment
\raggedleftRight alignment

To add the alignment command to each cell, thus adjusting the column cell horizontal alignment, we use the following syntax before the table column type declaration:

This will ensure that some_command is inserted before every column cell content.

Table with wider column and custom alignment#

To use bigger column width and center alignment in first and last column in the above table, we can use the following code:

Click to show the code.
\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{array}

\begin{document}

\begin{table}[t]
    \centering
    \begin{tabular}{>{\centering}p{0.25\textwidth}p{0.2\textwidth}p{0.2\textwidth}>{\centering\arraybackslash}p{0.15\textwidth}}
        \toprule
        \multirow{2}{*}{Country} & \multicolumn{2}{c}{GDP} & Change \\
        \cmidrule{2-3} \\
        {} & 2010 & 2015 & {} \\
        \midrule
        United States & 14964.400 & 18036.650 & 20.53\% \\
        China & 5812.464 & 11226.186 & 93.14\% \\
        Japan & 5793.455 & 4382.420 & -24.36\% \\
        Germany & 3309.668 & 3365.293 & 1.68\% \\
        United Kingdom & 2246.079 & 2863.304 & 27.48\% \\
        \bottomrule
    \end{tabular}
\end{table}

\end{document}

For table cells where the horizontal alignment is not explictly defined, the default is left alignment. Note that if you want to change the alignment of last column, the \arraybackslash command must also be supplied, or you will encounter errors when compiling the LaTeX source code:

Extra alignment tab has been changed to \cr.

The reason of this error can be found here if you are interested.

The created table will be like the following:

Defining new column types to simplify things#

Writing column width and alignment in the tabular column type option is rather lengthy and error-prone. We can define new column types and use them in the column type option to simplify things.

The \newcolumntype command from array package is used to define new column types, for example:

\newcolumntype{A}{>{\centering}p{0.25\textwidth}}

Using this command, our code now becomes more concise and readable:

Click to show the code.

\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{array}

\begin{document}

\newcolumntype{A}{>{\centering}p{0.25\textwidth}}
\newcolumntype{B}{p{0.2\textwidth}}
\newcolumntype{C}{>{\centering\arraybackslash}p{0.15\textwidth}}
\begin{table}[t]
    \centering
    \begin{tabular}{ABBC}
        \toprule
        \multirow{2}{*}{Country} & \multicolumn{2}{c}{GDP} & Change \\
        \cmidrule{2-3} \\
        {} & 2010 & 2015 & {} \\
        \midrule
        United States & 14964.400 & 18036.650 & 20.53\% \\
        China & 5812.464 & 11226.186 & 93.14\% \\
        Japan & 5793.455 & 4382.420 & -24.36\% \\
        Germany & 3309.668 & 3365.293 & 1.68\% \\
        United Kingdom & 2246.079 & 2863.304 & 27.48\% \\
        \bottomrule
    \end{tabular}
\end{table}

\end{document}

References#