使用 tabularx 時如何在表格中設定垂直居中文字並新增輸入?

使用 tabularx 時如何在表格中設定垂直居中文字並新增輸入?

如何在表格中垂直居中文本,例如“作者 1(年份)”,以及如何在表格中的單字之間添加輸入,以便文本彼此位於同一位置,例如戰爭、革命和內亂等單字?

\begin{table}[H]
\caption{Three levels of political risks} 
\label{tab:rcpol}
\footnotesize
\begin{tabularx}{\linewidth}{@{} c *{3}{>{\centering\arraybackslash}X} @{}}
\toprule 
\textbf{References} & \textbf{Macro risks} & \textbf{Meso risks} & \textbf{Micro risks}  \\
\midrule
Author 1 (Year)       
    &  Corruption   
        &  Government intervention
             &  Expropriation; 
                Nationalisation of assets
    \\ \midrule
Author 2 (Year)     
    &  War; Revolution; Civil disorders
        &   \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth
                          \centering\arraybackslash}X}{%
        Inconsistent government policies } 
    \\ 

\bottomrule
\end{tabularx}
\end{table}

在此輸入影像描述

答案1

X類型列內,您可以用來\linebreak插入換行符號。在普通儲存格內,您可以使用\begin{tabular}{@{} <col> @{}}<content>\end{tabular}構造來插入換行符(或\makecell從套件中使用makecell,效果相同)。

如果您想在環境\\中使用 的定義\centering,您也可以>{\centering\let\mynewline\\\arraybackslash}X在序言和表中使用\mynewline而不是\linebreak

至於X類型單元格的垂直對齊方式:您可以更改垂直居中內容的定義,\tabularxcolumn以使用m而不是p類型列。

\documentclass[]{article}

\usepackage{tabularx}
\usepackage{booktabs}

% change the column type of tabularx to use `m` instead of `p` for the vertical
% centering:
\renewcommand\tabularxcolumn[1]{m{#1}}

\begin{document}
\begin{table}
\caption{Three levels of political risks} 
\label{tab:rcpol}
\footnotesize
\begin{tabularx}{\linewidth}{@{} c *{3}{>{\centering\arraybackslash}X} @{}}
\toprule 
\textbf{References} & \textbf{Macro risks} & \textbf{Meso risks} & \textbf{Micro risks}  \\
\midrule
Author 1 (Year)       
    &  Corruption   
        &  Government intervention
             &  Expropriation; 
                Nationalisation of assets
    \\ \midrule
Author 2 (Year)     
    &  War; Revolution; Civil disorders
        &   \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth
                          \centering\arraybackslash}X}{%
        Inconsistent government policies } 
    \\ 
    author & En-\linebreak forcing\linebreak line\linebreak breaks 
    \\
    \begin{tabular}[]{@{}c@{}}
      line\\breaks\\in\\a\\normal\\cell
    \end{tabular}
    & other cell & other cell
    \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}

在此輸入影像描述

相關內容