表减速

表减速

在我的文档中,我有一个表格,我想使用\setlength{\tabcolsep}{18pt},但由于还有一个表格,这也会影响另一个表格,我不想更改它。因此,我想创建另一个只有 1 个表格的“.tex”文件,然后将其包含在主文件中,但这行不通。我该怎么办?实际上,我的主要目的是在不更改字体大小的情况下将表格扩展到列宽。

\setlength{\tabcolsep}{18pt}
\begin{tabular}{|cc|cc|}
\hline
\multicolumn{2}{|c|}{\textbf{Orbital positions}}     & \multicolumn{2}{c|}{\textbf{Total solar irradiance}}        \\ \hline
\multicolumn{1}{|c|}{\textbf{Earth}} & \textbf{Moon} & \multicolumn{1}{c|}{\textbf{near side}} & \textbf{far side} \\ \hline
\multicolumn{1}{|c|}{perihelion} & perigee & \multicolumn{1}{c|}{1,401.65} & 1,415.54 \\ \hline
\multicolumn{1}{|c|}{perihelion} & apogee  & \multicolumn{1}{c|}{1,400.83} & 1,416.36 \\ \hline
\multicolumn{1}{|c|}{aphelion}   & perigee & \multicolumn{1}{c|}{1,311.18} & 1,323.74 \\ \hline
\multicolumn{1}{|c|}{aphelion}   & apogee  & \multicolumn{1}{c|}{1,310.44} & 1,324.49 \\ \hline
\end{tabular}

此外,我没有写,\begin{table}因为当我写它时字体类型正在改变。

答案1

为了将指令的范围限制\setlength{\tabcolsep}{18pt}在单个tabular环境中,我建议您在 \end{tabular}` 之前立即放置一条指令\begingroup\setlength{\tabcolsep}{18pt}\endgroup instruction immediately after

假设您确实需要环境中的垂直规则tabular,我还建议您\begin{tabular}{|cc|cc|}用替换\begin{tabular}{|c|c|c|c|},因为这样做可以让您摆脱十个\multicolumn{1}{...}{...}包装器并显著减少代码混乱。

如果您的目标是让四列表格占据当前列的整个宽度,并且您不介意四列宽度相等,我建议您使用环境tabularx并将其目标宽度设置为\columnwith。无需尝试各种试验值\tabcolsep

在此处输入图片描述

\documentclass{article} 

\usepackage{tabularx} % for 'tabularx' env. and 'X' col. type
% Define a centered version of 'X' column type:
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}

% Limit scope of '\setlength' instruction to current TeX group:
\begingroup 
\setlength{\tabcolsep}{18pt}
\noindent
\begin{tabular}{|c|c|c|c|}
\hline
\multicolumn{2}{|c|}{\textbf{Orbital positions}} & 
\multicolumn{2}{ c|}{\textbf{Total solar irradiance}} \\ \hline
\textbf{Earth} & \textbf{Moon} & 
\textbf{near side} & \textbf{far side} \\ \hline
perihelion & perigee & 1,401.65 & 1,415.54 \\ \hline
perihelion & apogee  & 1,400.83 & 1,416.36 \\ \hline
aphelion   & perigee & 1,311.18 & 1,323.74 \\ \hline
aphelion   & apogee  & 1,310.44 & 1,324.49 \\ \hline
\end{tabular}
\endgroup


\bigskip\noindent
\begin{tabularx}{\columnwidth}{|C|C|C|C|}
\hline
\multicolumn{2}{|c|}{\textbf{Orbital positions}} & 
\multicolumn{2}{ c|}{\textbf{Total solar irradiance}} \\ \hline
\textbf{Earth} & \textbf{Moon} & 
\textbf{near side} & \textbf{far side} \\ \hline
perihelion & perigee & 1,401.65 & 1,415.54 \\ \hline
perihelion & apogee  & 1,400.83 & 1,416.36 \\ \hline
aphelion   & perigee & 1,311.18 & 1,323.74 \\ \hline
aphelion   & apogee  & 1,310.44 & 1,324.49 \\ \hline
\end{tabularx}

\end{document}

答案2

不清楚为什么你喜欢增加\tabcolsep。我宁愿定义列的宽度,例如通过使用array包:

\documentclass{article}
\usepackage{array}

\begin{document}
    \begin{table}[ht]
\begin{tabular}{|*4{w{c}{6em}|}}
    \hline
\multicolumn{2}{|c|}{\textbf{Orbital positions}}     
                & \multicolumn{2}{c|}{\textbf{Total solar irradiance}}      \\  \hline
\textbf{Earth}  & \textbf{Moon} & \textbf{near side}    & \textbf{far side} \\  \hline
perihelion      & perigee       & 1,401.65              & 1,415.54          \\  \hline
perihelion      & apogee        & 1,400.83              & 1,416.36          \\  \hline
aphelion        & perigee       & 1,311.18              & 1,323.74          \\  \hline
aphelion        & apogee        & 1,310.44              & 1,324.49          \\ \hline
\end{tabular}    
    \end{table}
\end{document}

在此处输入图片描述

笔记: 在浮动中插入表格肯定不会改变表格中使用的字体。

答案3

与。{NiceTabular}nicematrix

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{cccc}[hvlines,columns-width=6em]
\RowStyle[nb-rows=2]{\bfseries}
\Block{1-2}{Orbital positions} & & 
\Block{1-2}{Total solar irradiance} \\ 
Earth      & Moon    & near side & far side \\ 
perihelion & perigee & 1,401.65  & 1,415.54 \\ 
perihelion & apogee  & 1,400.83  & 1,416.36 \\ 
aphelion   & perigee & 1,311.18  & 1,323.74 \\ 
aphelion   & apogee  & 1,310.44  & 1,324.49 \\ 
\end{NiceTabular}    

\end{document}

上述代码的输出

答案4

使用包可以很容易地实现您想要的功能tabularray

\documentclass{article} 
\usepackage{tabularray}
\usepackage{caption}

\begin{document}
Some text only to show the table has the width of the text line.
It is very easy to achieve what you want using \texttt{tabularray} package:
\begin{center}
\captionof{table}{My caption}
\begin{tblr}{colspec={*4{X[c]}},
    hlines, vlines,
    row{1-2}={font=\bfseries},
    }
\SetCell[c=2]{c}Orbital positions & &
\SetCell[c=2]{c}Total solar irradiance & \\ 
Earth & Moon & near side & far side \\ 
perihelion & perigee & 1,401.65 & 1,415.54 \\ 
perihelion & apogee  & 1,400.83 & 1,416.36 \\ 
aphelion   & perigee & 1,311.18 & 1,323.74 \\ 
aphelion   & apogee  & 1,310.44 & 1,324.49 \\ 
\end{tblr}
\end{center}
\end{document}

在此处输入图片描述

相关内容