Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Q42如何在设置表格列对齐方式的同时设置表格列宽? #50

Open
Lihua1990 opened this issue Mar 8, 2020 · 7 comments
Open

Comments

@Lihua1990
Copy link
Collaborator

No description provided.

@EthanDeng
Copy link
Owner

EthanDeng commented Mar 9, 2020

之前的问题 #42 这里原来其实提供了怎么对齐的方法,但是不太特别全面,也不是最好。

我找了下之后最后给出下面的几种对齐方式:

  1. 纵向居中对齐方式:
    • L: 居左纵向居中对齐
    • C: 居中纵向居中对齐
    • R: 居右纵向居中对齐
  2. 纵向顶端对齐方式:
    • X: 居左纵向顶端对齐
    • Y: 居中纵向顶端对齐
    • Z: 居右纵向顶端对齐

分别使用 array 宏包中的 \newcolumntype 构造新对齐方式,利用 ragged2e 宏包中的 \RaggedLeft(右对齐)、\RaggedRight(左对齐)设置对齐方式,另外增加 \hspace{0pt} 使得单元格内可以自行换行(第三张表格)。设置代码如下:

\newcolumntype{L}[1]{>{\RaggedRight\hspace{0pt}}m{#1}} % middle + left
\newcolumntype{C}[1]{>{\centering\arraybackslash\hspace{0pt}}m{#1}} % middle + center
\newcolumntype{R}[1]{>{\RaggedLeft\hspace{0pt}}m{#1}} % middle + right

\newcolumntype{X}[1]{>{\RaggedRight\hspace{0pt}}p{#1}} % top + left
\newcolumntype{Y}[1]{>{\centering\arraybackslash\hspace{0pt}}p{#1}} % top + center
\newcolumntype{Z}[1]{>{\RaggedLeft\hspace{0pt}}p{#1}} % top + right

根据原文的说法:

Instead of \raggedright, you could also use the enhanced command \RaggedRight of the ragged2e package, which allows hyphenation. Further \arraybackslash, which restores the behavior of \ (changed by \raggedright), is no longer needed.

关于 \hspace{0pt} 的解释:

I'm further inserting \hspace{0pt} because otherwise there could be hyphenation problems: TeX doesn't hyphenate the first word in a box. Inserting zero space works around that.

@EthanDeng
Copy link
Owner

EthanDeng commented Mar 9, 2020

完整的 MWE 如下:

\documentclass{article}

\usepackage[a4paper, margin=1in]{geometry}
\usepackage{newtxtext}

\usepackage{array,booktabs,ragged2e}

\newcolumntype{L}[1]{>{\RaggedRight\hspace{0pt}}m{#1}} % middle + left
\newcolumntype{C}[1]{>{\centering\arraybackslash\hspace{0pt}}m{#1}} % middle + center
\newcolumntype{R}[1]{>{\RaggedLeft\hspace{0pt}}m{#1}} % middle + right

\newcolumntype{X}[1]{>{\RaggedRight\hspace{0pt}}p{#1}} % top + left
\newcolumntype{Y}[1]{>{\centering\arraybackslash\hspace{0pt}}p{#1}} % top + center
\newcolumntype{Z}[1]{>{\RaggedLeft\hspace{0pt}}p{#1}} % top + right

% ref: https://tex.stackexchange.com/questions/7347/fixed-column-width-table-with-text-left-aligned-in-cells
\begin{document}

\begin{table}
  \centering
  \caption{LCR with middle align}
  \begin{tabular}{L{2.5cm}C{2.5cm}R{2.5cm}}
    \toprule
    Reconstruction Strategy (Left)  & Information (Center) & bb(\%) (Right) \\
    \midrule
    Classic                 & 3342 voxels & 68 \%   \\ 
    VC                      & 426 voxels & 87 \%   \\ 
    $V_m=7$             & 94745 voxels & 96 \%   \\ 
    \bottomrule
  \end{tabular}
\end{table}


\begin{table}
  \centering
  \caption{LCR with top align}
  \begin{tabular}{X{2.5cm}Y{2.5cm}Z{2.5cm}}
    \toprule
    Reconstruction Strategy (Left)  & Information (Center) & bb(\%) (Right) \\
    \midrule
    Classic                 & 3342 voxels & 68 \%   \\ 
    VC                      & 426 voxels & 87 \%   \\ 
    $V_m=7$             & 94745 voxels & 96 \%   \\ 
    \bottomrule
  \end{tabular}
\end{table}


\begin{table}
  \centering
  \caption{LCR with auto hypenation}
  \begin{tabular}{L{2.5cm}C{2.5cm}R{2.5cm}}
    \toprule
    Reconstructionxxxxxx Strategy (Left)  & Information (Center) & bb(\%) (Right) \\
    \midrule
    Classic                 & 3342 voxels & 68 \%   \\ 
    VC                      & 426 voxels & 87 \%   \\ 
    $V_m=7$             & 94745 voxels & 96 \%   \\ 
    \bottomrule
  \end{tabular}
\end{table}

\end{document} 

效果如下:

1

2

3

参考资料:

  1. fixed-column-width-table-with-text-left-aligned-in-cells
  2. ctan/pkage/array
  3. What is \arraybackslash doing on my table column?

@Lihua1990
Copy link
Collaborator Author

你之前提到的用小数点对齐怎么设置?

@EthanDeng
Copy link
Owner

我昨天说的是用 @ 方式写,也就是下面的代码

\documentclass[12pt]{article}

\usepackage{booktabs}

\begin{document}

\begin{table}[!htb]
  \centering
  \begin{tabular}{r@{.}lc}
  \toprule
  \multicolumn{2}{c}{Heading} & Modification\\
  \midrule
    3&141 & Text    \\
   16&2   & $\alpha$\\
  123&456 & English \\
  \bottomrule
  \end{tabular}
\end{table}

\end{document}

这个 @ 也就是取消两边的距离,然后用 . 分隔,然后整数部分的列居右,小数部分居左,效果图如下:

QQ截图20200309200152

@EthanDeng
Copy link
Owner

但是这个方法有个不好的地方是,需要将数字这列拆分成两列,我刚看了下现在有两种其他方法,第一种用 dcolumn 宏包,第二种,siunitx 宏包,我自己比较了下,觉得我个人偏向于使用前者。对应于它们各自宏包的用法见下:

  1. dcolumn 宏包:How to align numbers on decimal point in latex
  2. siunitx 宏包:Number Alignment in Data Table

我把我前面的代码增加了 dcolumn 宏包,然后增加了一个示例,见下:

\documentclass{article}

\usepackage[a4paper, margin=1in]{geometry}
\usepackage{newtxtext}

\usepackage{array,booktabs,ragged2e}

\usepackage{dcolumn}

\newcolumntype{d}[1]{D{.}{.}{#1}} % input sep+output sep+{x.y}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro

\newcolumntype{L}[1]{>{\RaggedRight\hspace{0pt}}m{#1}} % middle + left
\newcolumntype{C}[1]{>{\centering\arraybackslash\hspace{0pt}}m{#1}} % middle + center
\newcolumntype{R}[1]{>{\RaggedLeft\hspace{0pt}}m{#1}} % middle + right

\newcolumntype{X}[1]{>{\RaggedRight\hspace{0pt}}p{#1}} % top + left
\newcolumntype{Y}[1]{>{\centering\arraybackslash\hspace{0pt}}p{#1}} % top + center
\newcolumntype{Z}[1]{>{\RaggedLeft\hspace{0pt}}p{#1}} % top + right

% ref: https://tex.stackexchange.com/questions/7347/fixed-column-width-table-with-text-left-aligned-in-cells
\begin{document}

\begin{table}
  \centering
  \caption{LCR with middle align}
  \begin{tabular}{L{2.5cm}C{2.5cm}R{2.5cm}}
    \toprule
    Reconstruction Strategy (Left)  & Information (Center) & bb(\%) (Right) \\
    \midrule
    Classic                 & 3342 voxels & 68 \%   \\ 
    VC                      & 426 voxels & 87 \%   \\ 
    $V_m=7$             & 94745 voxels & 96 \%   \\ 
    \bottomrule
  \end{tabular}
\end{table}


\begin{table}
  \centering
  \caption{LCR with top align}
  \begin{tabular}{X{2.5cm}Y{2.5cm}Z{2.5cm}}
    \toprule
    Reconstruction Strategy (Left)  & Information (Center) & bb(\%) (Right) \\
    \midrule
    Classic                 & 3342 voxels & 68 \%   \\ 
    VC                      & 426 voxels & 87 \%   \\ 
    $V_m=7$             & 94745 voxels & 96 \%   \\ 
    \bottomrule
  \end{tabular}
\end{table}


\begin{table}
  \centering
  \caption{LCR with auto hypenation}
  \begin{tabular}{L{2.5cm}C{2.5cm}R{2.5cm}}
    \toprule
    Reconstructionxxxxxx Strategy (Left)  & Information (Center) & bb(\%) (Right) \\
    \midrule
    Classic                 & 3342 voxels & 68 \%   \\ 
    VC                      & 426 voxels & 87 \%   \\ 
    $V_m=7$             & 94745 voxels & 96 \%   \\ 
    \bottomrule
  \end{tabular}
\end{table}


\begin{table}
  \centering
  \caption{LCR with auto hypenation}
  \begin{tabular}{L{2.5cm}C{2.5cm}R{2.5cm}d{4.3}}
    \toprule
    Reconstructionxxxxxx Strategy (Left)  & Information (Center) & bb(\%) (Right) & \mc{Decimal} \\
    \midrule
    Classic                 & 3342 voxels & 68 \%   & 333.30\\ 
    VC                      & 426 voxels & 87 \%   & 30\\ 
    $V_m=7$             & 94745 voxels & 96 \%   & 28.1\\ 
    \bottomrule
  \end{tabular}
\end{table}

\begin{table}
  \centering
  \caption{LCR with decimal align}
  \begin{tabular}{L{2.5cm}C{2.5cm}R{2.5cm}d{4.3}}
    \toprule
    Reconstruction Strategy (Left)  & Information (Center) & bb(\%) (Right) & \mc{Decimal} \\
    \midrule
    Classic                 & 3342 voxels & 68 \%   & 333.30\\ 
    VC                      & 426 voxels & 87 \%   & 30\\ 
    $V_m=7$             & 94745 voxels & 96 \%   & 28.1\\ 
    \bottomrule
  \end{tabular}
\end{table}

\end{document} 

导言区就增加了 dcolumn 后面三行,然后示例增加了最后一个,最后一个的效果图:

QQ截图20200309201000

@EthanDeng
Copy link
Owner

补充d{4.3} 的意思是,设定这一列的宽度为小数点前 4 个数字的宽度+ 小数点后 3 个数字的宽度,超过这个位数的话,会溢出到左右两边,而不会自动调整表格的宽度。

@Lihua1990
Copy link
Collaborator Author

我昨天说的是用 @ 方式写,也就是下面的代码

\documentclass[12pt]{article}

\usepackage{booktabs}

\begin{document}

\begin{table}[!htb]
  \centering
  \begin{tabular}{r@{.}lc}
  \toprule
  \multicolumn{2}{c}{Heading} & Modification\\
  \midrule
    3&141 & Text    \\
   16&2   & $\alpha$\\
  123&456 & English \\
  \bottomrule
  \end{tabular}
\end{table}

\end{document}

这个 @ 也就是取消两边的距离,然后用 . 分隔,然后整数部分的列居右,小数部分居左,效果图如下:

QQ截图20200309200152
这个虽然latex代码很方便,但需要改原始数据太麻烦了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants