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

推荐订阅源

D
Docker
Simon Willison's Weblog
Simon Willison's Weblog
H
Help Net Security
F
Fortinet All Blogs
H
Heimdal Security Blog
S
Schneier on Security
L
LangChain Blog
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
NISL@THU
NISL@THU
P
Palo Alto Networks Blog
J
Java Code Geeks
博客园 - 【当耐特】
The Last Watchdog
The Last Watchdog
W
WeLiveSecurity
www.infosecurity-magazine.com
www.infosecurity-magazine.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
I
InfoQ
Recorded Future
Recorded Future
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
CERT Recently Published Vulnerability Notes
T
Tenable Blog
腾讯CDC
C
Check Point Blog
量子位
M
MIT News - Artificial intelligence
GbyAI
GbyAI
罗磊的独立博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
B
Blog
小众软件
小众软件
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
V2EX - 技术
V2EX - 技术
T
Threatpost
Engineering at Meta
Engineering at Meta
Attack and Defense Labs
Attack and Defense Labs
T
Tailwind CSS Blog
S
Securelist
The Cloudflare Blog
博客园 - 叶小钗
L
LINUX DO - 最新话题
T
Troy Hunt's Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
爱范儿
爱范儿

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 Generating Table of Contents for Markdown with Tagbar Convert Python Script to Exe on Windows with Pyinstaller Ubuntu on Windows Missing after Windows Update 使用代理加速 Mac 终端下载速度 My Experience with Several Zsh Plugin Managers 深圳租房小记 How to Install zplug inside Docker Container Why don't settings inside bashrc or bash_profile take effect? Setting Up Locale in Linux 谷歌 Adsense 申请及在 Hugo 中的配置 How to Write Algorithm Pseudo Code in LaTeX Nifty Nvim Techniques That Make My Life Easier -- Series 4 A Few Grammar Questions in Writing How to Read and Write Images with Unicode Paths in OpenCV Creating A Professional Table in LaTeX with booktabs How to Create Proper Folding for Vim/Nvim Configuration Linux Tips and Tricks -- s1 JPEG Image Orientation and Exif How Do I Show the Current File Path In Neovim? JPEG Image Quality in PIL Difference between view, reshape, transpose and permute in PyTorch Convert PIL or OpenCV Image to Bytes without Saving to Disk Fast Movement and Navigation Inside Vim or Neovim Unintuitive Behaviour of Case Sensitivity in Python glob Binding Keys in Zsh 几把机械键盘试用体验 Nvim Autocompletion with Deoplete Converting Markdown to Beautiful PDF with Pandoc Exclusive and Inclusive Motion in Neovim/Vim Nifty Nvim Techniques Which Make My Life Easier -- Series 3 Why Doesn't Jedi Autocompletion Work for Some Methods Vim-like Editing inside Browser Markdown 生成 HTML 时汉字之间出现多余空格问题 小米 9 安装谷歌商店(Google Play Store)与相关配置 Create Mappings That Take A Count in Neovim Spell Checking in Nvim English Words Completion inside Neovim/Vim How to Use Python Inside Vim Script with Neovim Nifty Little Nvim Techniques to Make My Life Easier -- Series 2 Setting up Ultisnips for Neovim Mac 上罗技 M590 鼠标设置 Nifty Little Nvim Techniques to Make My Life Easier -- Series 1 A Complete Guide on Writing LaTeX with Vimtex in Neovim Manipulating Images with Alpha Channels in Pillow Sublime Text Regular Expression Cheat Sheet Cropping Rotated Rectangles from Image with OpenCV Boosting Your Productivity on Terminal with Zsh and Plugins 最新版 Rime 输入法使用 (2022 更新) Display Image with Pillow inside Ubuntu on Windows Faster Directory Navigation with z.lua Cmder Advanced Configurations Nvim-qt Settings on Windows 10 Tmux Plugin Install and Management How to Debug Python Code in Terminal Markdown Writing and Previewing in Neovim -- A Complete Guide Line Number Settings for More Efficient Movement in Neovim 两个大规模中文语料库介绍以及处理 Windows 系统下几款程序员不可不用的神器 我的 2018 阅读清单 A Complete Guide to Neovim Configuration for Python Development How Is Newline Handled in Python and Various Editors? Two Issues Related to ImageFont Module in PIL 在 Listary 中调用 GoldenDict 或欧路词典查单词 Reading and Writing Text Files on Windows The Mathematics behind Font Shapes --- Bézier Curves and More 快速识别图片字体:字体识别工具介绍 Deoplete Failed to Load at Startup after Updating Python neovim Package What Is The Difference between pip, pip3 and pip3.6 Shipped with Anaconda3? Windows 10 系统下 Neovim 安装与配置 How to Use shutil.move() on Windows and Linux
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#