如何在 Latex 中制作这样的表格?

如何在 Latex 中制作这样的表格?

我对 Latex 还很陌生,我想制作如下所示的表格(可能在“系列”列中有更多行):

在此处输入图片描述

有人能帮我吗?我尝试在线查找,但代码失败了(我使用的是 Rmarkdown,但到目前为止 Latex 代码一直有效)。

这是我尝试过的代码(及变体):

\begin{center}
\begin{tabular}{ | m{5cm} | m{1cm}| m{1cm} | } 
\hline
cell1 dummy text dummy text dummy text& cell2 & cell3 \\ 
\hline
cell1 dummy text dummy text dummy text & cell5 & cell6 \\ 
\hline
cell7 & cell8 & cell9 \\ 
\hline
\end{tabular}
\end{center} 

答案1

\begin{center}
    \begin{tabular}{llc} 
        \hline
        & Series & R-squared \\
        \hline
        & cell2 & cell3 \\
        & Industrial Turnover Index, Manufacturing & cell3 \\
        Factor 1 & cell2 & cell3 \\
        & cell2 & cell3 \\
        & cell2 & cell3 \\
        \hline
    \end{tabular} 
\end{center}

答案2

例如,您可以通过拥有一个tabularx包含两列的环境来实现这一点。第一列是左对齐的因子列。第二列是 X 列。

第二列用于容纳另一个tabularx环境。另一个tabularx的第一列是系列列,是 X 列。另一个的tabularx第二列是 R 平方列,是居中列,其宽度等于短语“R 平方”的宽度。

整个内部环境的宽度tabularx\hsize与周围 X 柱的宽度相同。

为了获得正确的水平间距,您可能希望将其设置\tabcolsep为零宽度,并@根据您的喜好用于在列之间插入水平空间。

您可能还希望使用 booktabs 包的一些宏来绘制不同粗细的水平线。

在第一次看过问题中提供的图像后,我可能会这样处理这个问题。

\documentclass{article}
\usepackage{booktabs, tabularx}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\newlength\tabcolsepcopy
\newcommand\Rsquzaredhsize{}
\newbox\tempbox

\begin{document}

\noindent\hrulefill Width of line of text \hrulefill

\bigskip

\begingroup
\tabcolsepcopy=\tabcolsep
\tabcolsep=0ex
\noindent
\begin{tabularx}{\textwidth}{l@{\hspace{2\tabcolsepcopy}}X}
\toprule&%
{%
  \begin{tabularx}{\hsize}[c]{X@{\hspace{2\tabcolsepcopy}}c}%
  Series&R-squared%
  \setbox\tempbox\hbox{R-squared}%
  \xdef\Rsquzaredhsize{\the\wd\tempbox}%
  \end{tabularx}%
}%
\\
\midrule 
Factor 1%
&%
{%
  \begin{tabularx}{\hsize}[c]{X@{\hspace{2\tabcolsepcopy}}C{\Rsquzaredhsize}}%
  Producer Prices in Industry&0.67\\
  Harmonised Index of Consumer Prices&0.56\\
  Industrial Turnover Index, Manufacturing&0.53\\
  Compensation of Employees&0.59\\
  Gross Fixed Capital Formation Price Index&0.48%
  \end{tabularx}%
}%
\\
\midrule 
Factor 2%
&%
{%
  \begin{tabularx}{\hsize}[c]{X@{\hspace{2\tabcolsepcopy}}C{\Rsquzaredhsize}}%
  Cost of Borrowing for Households for House Purchase&0.49\\
  6-month Euribor&0.45\\
  1-year Euribor&0.45\\
  3-month Euribor&0.44\\
  Long-term Interest Rate Belgium&0.43%
  \end{tabularx}%
}%
\\
\midrule 
Factor 3%
&%
{%
  \begin{tabularx}{\hsize}[c]{X@{\hspace{2\tabcolsepcopy}}C{\Rsquzaredhsize}}%
  Government Spending Italy&0.61\\
  Unit Labour Cost Germany&0.61\\
  Government Spending Finland&0.61\\
  Unit Labour Cost Luxembourg&0.60\\
  Unit Labour Cost Italy&0.60%
  \end{tabularx}%
}%
\\
\midrule
Factor 4%
&%
{%
  \begin{tabularx}{\hsize}[c]{X@{\hspace{2\tabcolsepcopy}}C{\Rsquzaredhsize}}%
  Row 1 of factor 4&0.01\\
  Row 2 of factor 4&0.02\\
  Long long row 3 of factor 4 Long long row 3 of factor 4 Long long row 3 of factor 4
  Long long row 3 of factor 4 Long long row 3 of factor 4 Long long row 3 of factor 4
  Long long row 3 of factor 4 Long long row 3 of factor 4 Long long row 3 of factor 4
  Long long row 3 of factor 4 Long long row 3 of factor 4&0.03\\
  Row 4 of factor 4&0.04\\
  Row 5 of factor 4&0.05\\
  Row 6 of factor 4&0.06%
  \end{tabularx}%
}%
\\
\bottomrule
\end{tabularx}%
\endgroup
\end{document}

在此处输入图片描述

如果您不需要换行功能并且希望表格尽可能窄,那么请使用tabular-environments 而不是tabularx-environments:

\documentclass{article}
\usepackage{booktabs}

\newlength\tabcolsepcopy

\begin{document}

\noindent\hrulefill Width of line of text \hrulefill

\bigskip

\begingroup
\tabcolsepcopy=\tabcolsep
\tabcolsep=0ex
\noindent
\begin{tabular}{l@{\hspace{2\tabcolsepcopy}}l@{\hspace{2\tabcolsepcopy}}c}
\toprule&Series&R-squared
\\
\midrule 
Factor 1%
&%
\begin{tabular}[c]{l}%
Producer Prices in Industry\\
Harmonised Index of Consumer Prices\\
Industrial Turnover Index, Manufacturing\\
Compensation of Employees\\
Gross Fixed Capital Formation Price Index%
\end{tabular}%
&%
\begin{tabular}[c]{c}%
0.67\\
0.56\\
0.53\\
0.59\\
0.48%
\end{tabular}%
\\
\midrule
Factor 2%
&%
\begin{tabular}[c]{l}%
Cost of Borrowing for Households for House Purchase\\
6-month Euribor\\
1-year Euribor\\
3-month Euribor\\
Long-term Interest Rate Belgium%
\end{tabular}%
&%
\begin{tabular}[c]{c}%
0.49\\
0.45\\
0.45\\
0.44\\
0.43%
\end{tabular}%
\\
\midrule
Factor 3%
&%
\begin{tabular}[c]{l}%
Government Spending Italy\\
Unit Labour Cost Germany\\
Government Spending Finland\\
Unit Labour Cost Luxembourg\\
Unit Labour Cost Italy%
\end{tabular}%
&%
\begin{tabular}[c]{c}%
0.61\\
0.61\\
0.61\\
0.60\\
0.60%
\end{tabular}%
\\
\midrule
Factor 4%
&%
\begin{tabular}[c]{l}%
Row 1 of factor 4\\
Row 2 of factor 4\\
Row 3 of factor 4\\
Row 4 of factor 4\\
Row 5 of factor 4\\
Row 6 of factor 4%
\end{tabular}%
&%
\begin{tabular}[c]{c}%
0.01\\
0.02\\
0.03\\
0.04\\
0.05\\
0.06%
\end{tabular}%
\\
\bottomrule
\end{tabular}
\endgroup
\end{document}

在此处输入图片描述

相关内容