多行 tabularx-cell 垂直居中時邊距不均勻

多行 tabularx-cell 垂直居中時邊距不均勻

我讀了很多關於 tabularx-tables 中垂直居中的內容,並找到了一個通用的解決方案。但是,當單元格中有多行時,邊距似乎會關閉。

您可以看到,在頂行最右側的儲存格中,底部沒有邊距。此外,在第二行中,圓圈似乎沒有在中間完美對齊,而在第三行中看起來不錯。

我將不勝感激任何關於如何解決這個問題的想法!

在此輸入影像描述

\documentclass[a4paper,10pt,BCOR10mm,oneside,headsepline]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{wasysym}
\usepackage{enumitem}
\usepackage{color}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{typearea}
\usepackage[T1]{fontenc}
\usepackage[table]{xcolor}    % loads also »colortbl«
\areaset{17cm}{26cm}
\setlength{\topmargin}{-1cm}

\begin{document}

\newcolumntype{P}[1]{>{\centering\arraybackslash}m{#1}}
\renewcommand{\tabularxcolumn}[1]{m{#1}}

\setlength{\extrarowheight}{0.35cm}

\newcommand{\QO}{\huge{\textbf{$\Circle$}}\small}

\rowcolors{2}{gray!5}{gray!25}
\begin{tabularx}{\linewidth}{cX*{4}{P{1.5cm}}}%
   \rowcolor{gray!50}
   \textbf{1.} & \textbf{A very very very super long question, that is going to be split for sure\dots \newline (Something else)}
   & short & longer & little longer & super-duper mega very very very long \\[1em]%
   & short & \QO & \QO & \QO & \QO\\[1em]
   & Very long, will most propably span multiple lines. Might even mess up the formatting a little bit\dots & \QO & \QO & \QO & \QO\\[1em]
\end{tabularx}

\end{document}

答案1

這是該cellspace套件的一個解決方案,它定義了列中單元格頂部和底部的最小垂直填充,並帶有前綴為字母的說明符S。我藉此機會簡化了 的定義\QO

\documentclass[a4paper,10pt,BCOR10mm,oneside,headsepline]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{wasysym, amssymb, amsfonts, bm}
\usepackage{enumitem}
\usepackage{color}
\usepackage{tabularx}
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\usepackage{booktabs}
\usepackage{typearea}
\usepackage[T1]{fontenc}
\usepackage[table]{xcolor} % loads also »colortbl«
\areaset{17cm}{26cm}
\setlength{\topmargin}{-1cm}
\newcolumntype{P}[1]{>{\centering\arraybackslash}S{m{#1}}}
\renewcommand{\tabularxcolumn}[1]{m{#1}}

\newcommand{\QO}{\textbf{\huge\Circle}}

\usepackage{cellspace}
\setlength{\cellspacetoplimit}{2ex}
\setlength{\cellspacebottomlimit}{2ex}
\addparagraphcolumntypes{X}

\begin{document}

\rowcolors{2}{gray!5}{gray!25}
\begin{tabularx}{\linewidth}{cS{X}*{4}{P{1.5cm}}}%
   \rowcolor{gray!50}
   \textbf{1.} & \textbf{A very very very super long question, that is going to be split for sure\dots \newline (Something else)}
   & short & longer & little longer & super-duper mega very very very long \\
   & short & \QO & \QO & \QO & \QO \\
   & Very long, will most probably span multiple lines. Might even mess up the formatting a little bit\dots & \QO & \QO & \QO & \QO\\
\end{tabularx}

\end{document} 

在此輸入影像描述

答案2

圓圈定義會影響周圍的文本

\newcommand{\QO}{\textbf{\huge$\Circle$}}

將會是一個更好的定義,但如果你這樣做會更清楚

\newcommand{\QO}{!\dotfill\textbf{\huge$\Circle$}\dotfill!}

在此輸入影像描述

在這裡,您可以看到 .... 基線與 的基線對齊Short,但由於圓的大部分位於基線之上,因此它在單元格中顯得更高。

答案3

正如大衛卡萊爾在《他的回答,您的定義\QO將圓形放置在(印刷)基線上。因為它的尺寸很大(字面意思是\huge:),所以它最終看起來像是向上移動了很多。補救措施是將中心垂直居中於數學軸上。 (為了可視化數學軸的高度:數學符號-+放置在該軸上。)

在此輸入影像描述

前面的圖像是由生成的

ABC\dots$\bm\Circle$\dots\QOa\dots$-$\QOb$-$\dots XYZ

在哪裡

\newcommand{\QOa}{\textbf{\huge$\Circle$}} % OP's definition, improved
\newcommand{\QOb}{$\vcenter{\hbox{\huge$\bm\Circle$}}$} % proposed new def.

不管怎樣,如果把圓圈放在數學線上,表格看起來就很好(至少在我看來)。

在此輸入影像描述

\documentclass[paper=a4,  fontsize=10pt,
               BCOR=10mm, twoside=false, 
               headsepline]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{wasysym} % for "\Circle" macro
\usepackage{enumitem}
%%\usepackage{color} % don't load both 'color' and 'xcolor'
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{typearea}
\usepackage[T1]{fontenc}
\usepackage[table]{xcolor}    % loads also »colortbl«
\areaset{17cm}{26cm}
\setlength{\topmargin}{-1cm}

\usepackage{bm} % for "\bm" macro
%% center the circle (vertically) on the math axis:
\newcommand{\QO}{$\vcenter{\hbox{\huge$\bm\Circle$}}$}

\newcolumntype{P}[1]{>{\centering\arraybackslash}m{#1}}
\renewcommand{\tabularxcolumn}[1]{m{#1}}

\setlength{\extrarowheight}{0.35cm}

\begin{document}

\noindent % <-- don't forget this instruction
\rowcolors{2}{gray!5}{gray!25}
\begin{tabularx}{\linewidth}{cX*{4}{P{1.5cm}}}%
   \rowcolor{gray!50}
   \textbf{1.} 
   & \textbf{A very very very super long question, that is 
     going to be split for sure\dots   
   \newline (Something else)}
   & short & longer & little longer 
   & super-duper mega very very very long \\[1em]%
   & short & \QO & \QO & \QO & \QO\\[1em]
   & Very long, will most propably span multiple lines. 
     Might even mess up the formatting a little bit\dots 
           & \QO & \QO & \QO & \QO\\[1em]
\end{tabularx}

\end{document}

相關內容