目标是获得一张包含固定高度行,并将材料放置在垂直居中的某些单元格内。
为了寻找一种强大而简单的方法,我研究了这个想法:
\documentclass{article}
\usepackage{tabularx}
\begin{document}
\def\text{This is a text example inside a cell}
\newcommand{\cell}[2]{\parbox[t][#1][c]{4cm}{#2}} % <- '4cm' fixed!
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{tabularx}{\textwidth}{|X|C|}
\hline
\text & \cell{2cm}{\text} \\
\hline
\end{tabularx}
\end{document}
(如同这种方法使用minipage
。
此时,我想将每列的宽度放入我的\cell
命令中,以便用通用列宽值替换固定的“4cm”定义。
如果可能的话,我正在寻找的是获取使用的“当前”widh列寄存器tabularx,表格或者大批并将其用作参数。在单元格内部\parbox
使用时也需要这样做。\includegraphics [width = x]
例如,你可以使用帕拉科尔包裹:
\documentclass{article}
\usepackage{paracol}
\begin{document}
\def\text{This is a text example inside a cell}
\columnratio{0.8}
\begin{paracol}{2}
\parbox[t][2cm][c]{\csname pcol@columnwidth\number1\endcsname}{\text}
\end{paracol}
\end{document}
答案1
您可以使用 \linewidth:
\documentclass{article}
\usepackage{tabularx}
\begin{document}
\def\text{This is a text example inside a cell}
\newcommand{\cell}[2]{\parbox[t][#1][c]{\linewidth}{#2}} %
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{tabularx}{\textwidth}{|X|C|}
\hline
\text & \cell{0.5\linewidth}{\text} \\
\hline
\end{tabularx}
\end{document}
答案2
跳出固有思维模式...
向 Phelype 致敬,感谢她向我提供了有关\@wholewidth
和的线索\linethickness
。
\documentclass{article}
\usepackage{stackengine}
\newcount\cellwd
\newcount\cellsep
\cellwd=80
\cellsep=5
\makeatletter
%\linethickness{.1pt}
\newcommand\acell[1]{\noindent%
\framebox(\cellwd,\cellwd){\parbox{\the\numexpr\cellwd-\cellsep\relax pt}%
{\centering#1}}\kern\@wholewidth\ignorespaces}
\newcommand\cellrowskip{\\[\dimexpr-1pt+\@wholewidth\relax]}
\makeatother
\begin{document}
What precedes.
\acell{Here is a test of my parbox}
\acell{xyz}
\acell{Not sure if this is what is desired}
\cellrowskip
\acell{Ha-ha!}
\acell{We are almost there!}
\acell{Finis!}
What follows.
\end{document}
如果你想要一个更封装的实现,我引入了语法
\begin{celltable}[<linethickness>]{<cell width>}{<cell height>}{<2X cell sep>}
|{<cell data 1,1>} |{<cell-data 1,2>}
\cellrowskip
|{<cell data 2,1>} |{<cell-data 2,2>}
\end{celltable}
以下是 MWE:
\documentclass{article}
\usepackage{stackengine}
\newcount\cellwd
\newcount\cellht
\newcount\cellsep
\makeatletter
{\catcode`|=\active \gdef|{\acell}}
\newcommand\acell[1]{\noindent%
\framebox(\cellwd,\cellht){\parbox{\the\numexpr\cellwd-\cellsep\relax pt}%
{\centering#1}}\kern\@wholewidth\ignorespaces}
\newcommand\cellrowskip{\\[\dimexpr-1pt+\@wholewidth\relax]}
\newenvironment{celltable}[4][\@wholewidth]
{\cellwd=#2\relax
\cellht=#3\relax
\cellsep=#4\relax
\linethickness{#1}%
\vspace{#1}%
\par\catcode`\| \active
}{\par}
\makeatother
\begin{document}
What precedes.
\begin{celltable}[3pt]{70}{50}{5}
|{Here is a test of my parbox}
|{xyz}
|{Not sure if this is what is desired}
\cellrowskip
|{Ha-ha!}
|{We are almost there!}
|{Finis!}
\end{celltable}
What follows.
\end{document}