代码

代码

我可以使用什么技术让 TeX 计算一组框的最大高度,然后使用该高度排版所有框?

我想让 TeX 自动检测中间框并将其高度设置为行内邻居的最大值(水平对齐的框)。 在此处输入图片描述

  • 每个盒子都有一个静态宽度指明\makeform
  • 每个盒子都有一个动态高度在 下指定,因为中的 s\makeform的性质,即盒子可以垂直增长。当盒子垂直未充满时,为视觉外观指定最小高度。\par\parbox
  • 请注意,用户可以将任何东西放入框中,甚至是逐项列表。

代码

\documentclass{article}
\usepackage{fontspec}% xelatex
\usepackage{tikz}

% I would have preferred to use plain tex boxes for speed, but I opted TikZ because of preexisting knowledge

\makeatletter % simulate start of package code
% Backend boxmaker
\long\def\ABC@box#1#2#3{\tikz\node
[draw, line width=.4pt, inner sep=2pt,outer sep=0pt,align=left,minimum width=#2,minimum height=#3]
{\parbox{\dimexpr#2-4pt}{\raggedright\scriptsize#1}};}

% User-level boxmaker
\long\def\nodebox#1#2#3{\ABC@box{#1}{#2}{#3}\kern-.4pt}% kern used to horizontally shift boxes back by stroke width of frame

\gdef\ABCMANUFACTURERCONTACTINFORMATION#1{\gdef\ABCMANUFACTURERCONTACTINFORMATION{#1}} % Sets backend macro value

% Common minimum height for nodes so that empty forms look visually appealing
\newdimen\nodeboxminHT
\nodeboxminHT=1cm\relax

% Create form
\gdef\makeform{%
\nodebox{Manufacturer Information\\\ABCMANUFACTURERCONTACTINFORMATION{}}{.2\textwidth}{\nodeboxminHT}%
\nodebox{Manufacturer Information\\\ABCMANUFACTURERCONTACTINFORMATION{}}{.7\textwidth}{\nodeboxminHT}% redundant for demo only
\nodebox{Manufacturer Information\\\ABCMANUFACTURERCONTACTINFORMATION{}}{.2\textwidth}{\nodeboxminHT}% redundant for demo only
}
\makeatother % simulate end of package code

\begin{document}
\ABCMANUFACTURERCONTACTINFORMATION{Highly-specialized Special Ops}
\makeform
\end{document}

答案1

如果你可以从 Ti 切换Z 到 tcolorbox,那么这里有一个基于的解决方案这个答案

tcolorbox有一个选项equal height group,允许您将一捆箱子分组,使它们都具有相同的高度。您只需确保必须具有相同高度的箱子属于同一组。

我重新定义了你的\ABC@box宏来tcolorbox代替 TiZ,然后我给盒子制作宏添加了第四个参数,即盒子所属的组。

在此处输入图片描述

我猜测“简单”的方法需要将盒子保存在临时寄存器中,对其进行测量,然后写入它们。

\documentclass{article}
\usepackage{fontspec}% xelatex
\usepackage{tikz}

\usepackage[xparse]{tcolorbox}

% I would have preferred to use plain tex boxes for speed, but I opted TikZ because of preexisting knowledge

\makeatletter % simulate start of package code
% Backend boxmaker
\NewTotalTColorBox{\ABC@box}{+mmmm}{%
  size=fbox,
  sharp corners,
  colframe=black,
  colback=white,
  width=#2,
  height=#3,
  height plus=0pt plus 1fil,
  nobeforeafter,
  valign=center,
  equal height group=#4%
  }{#1}

% User-level boxmaker
\long\def\nodebox#1#2#3#4{\ABC@box{#1}{#2}{#3}{#4}\kern-.4pt}% kern used to horizontally shift boxes back by stroke width of frame

\gdef\ABCMANUFACTURERCONTACTINFORMATION#1{\gdef\ABCMANUFACTURERCONTACTINFORMATION{#1}} % Sets backend macro value

% Common minimum height for nodes so that empty forms look visually appealing
\newdimen\nodeboxminHT
\nodeboxminHT=1cm\relax

% Create form
\gdef\makeform#1{%
\nodebox{Manufacturer Information\\\ABCMANUFACTURERCONTACTINFORMATION{}}{.25\textwidth}{\nodeboxminHT}{#1}%
\nodebox{Manufacturer Information\\\ABCMANUFACTURERCONTACTINFORMATION{}}{ .5\textwidth}{\nodeboxminHT}{#1}% redundant for demo only
\nodebox{Manufacturer Information\\\ABCMANUFACTURERCONTACTINFORMATION{}}{.25\textwidth}{\nodeboxminHT}{#1}% redundant for demo only
}
\makeatother % simulate end of package code

\begin{document}
\ABCMANUFACTURERCONTACTINFORMATION{Highly-specialized Special Ops}
\noindent\makeform{group1}

\bigskip

\gdef\ABCMANUFACTURERCONTACTINFORMATION{Less content}
\noindent\makeform{group2}

\bigskip

\gdef\ABCMANUFACTURERCONTACTINFORMATION{Lorem ipsum dolor sit amet, consectetuer
  adipiscing elit. I don't remember the rest but didn't want to load a package.}
\noindent\makeform{group3}
\end{document}

答案2

这是一种技术,首先通过 LaTeX 临时框将所有内容排版在 TeX 寄存器框(永远不会打印)中\@tempboxa。计算最大高度,最后进行实际排版。

我本来可以使用 TeX 令牌寄存器,但是为了方便起见,我使用了包\BODY中的令牌寄存器。environ

代码

我添加了几行代码来尝试不同的情况。我还通过将行间距调整为一个框架笔划宽度(5pt)来重叠框行。

\documentclass{article}
\usepackage{fontspec}% xelatex
\usepackage{tikz}
\usepackage{environ}

% I would have preferred to use plain tex boxes for speed, but I opted TikZ because of preexisting knowledge

\makeatletter % simulate start of package code
% Backend boxmaker
\long\def\ABC@box#1#2#3{\tikz\node
[draw, line width=.4pt, inner sep=2pt,outer sep=0pt,align=left,minimum width=#2,minimum height=#3]
{\parbox{\dimexpr#2-4pt}{\raggedright\scriptsize#1}};}

% User-level boxmaker
\long\def\nodebox#1#2#3{\ABC@box{#1}{#2}{#3}\kern-.4pt}% kern used to horizontally shift boxes back by stroke width of frame

\gdef\ABCMANUFACTURERCONTACTINFORMATION#1{\gdef\ABCMANUFACTURERCONTACTINFORMATION{#1}} % Sets backend macro value

% Common minimum height for nodes so that empty forms look visually appealing
\newdimen\nodeboxminHT
\nodeboxminHT=1cm\relax

\NewEnviron{formrow}{\leavevmode\unskip\par\sbox\@tempboxa{\BODY}\nodeboxminHT=\the\ht\@tempboxa\BODY\ignorespaces}

% Create form
\gdef\makeform{%
  \parskip=0pt\relax
  \baselineskip=0ex\relax
  \lineskip=-.5pt\relax

  \begin{formrow}%
    \nodebox{Manufacturer Information\\\ABCMANUFACTURERCONTACTINFORMATION{}}{.2\textwidth}{\nodeboxminHT}%
    \nodebox{Manufacturer Information\\\ABCMANUFACTURERCONTACTINFORMATION{}}{.7\textwidth}{\nodeboxminHT}% redundant for demo only
    \nodebox{Manufacturer Information\\\ABCMANUFACTURERCONTACTINFORMATION{}}{.2\textwidth}{\nodeboxminHT}% redundant for demo only
  \end{formrow}

  \begin{formrow}
    \nodebox{Manufacturer Information\\\ABCMANUFACTURERCONTACTINFORMATION{}}{.2\textwidth}{\nodeboxminHT}%
    \nodebox{Manufacturer Information\\\ABCMANUFACTURERCONTACTINFORMATION{}}{.7\textwidth}{\nodeboxminHT}% redundant for demo only
    \nodebox{Manufacturer Information\\\ABCMANUFACTURERCONTACTINFORMATION{}}{.2\textwidth}{\nodeboxminHT}% redundant for demo only
  \end{formrow}
  \begin{formrow}
    \nodebox{Manufacturer Information\\\ABCMANUFACTURERCONTACTINFORMATION{}}{.2\textwidth}{\nodeboxminHT}%
    \nodebox{Manufacturer Information\\\ABCMANUFACTURERCONTACTINFORMATION{}}{.7\textwidth}{\nodeboxminHT}% redundant for demo only
    \nodebox{Manufacturer Information\\\ABCMANUFACTURERCONTACTINFORMATION{}}{.2\textwidth}{\nodeboxminHT}% redundant for demo only
  \end{formrow}

  \begin{formrow}
  \nodebox{Manufacturer Information\\\ABCMANUFACTURERCONTACTINFORMATION{}}{.2\textwidth}{\nodeboxminHT}%
  \hbox{\vbox{
    \hbox{\nodebox{Company Name}{.7\textwidth}{.5\nodeboxminHT}}%
    \hbox{\nodebox{Representative Name/Position}{.7\textwidth}{.5\nodeboxminHT}}%
  }}%
  \nodebox{Manufacturer Information\\\ABCMANUFACTURERCONTACTINFORMATION{}}{.2\textwidth}{\nodeboxminHT}% redundant for demo only
  \end{formrow}
  \begin{formrow}
    \nodebox{}{.2\textwidth}{\nodeboxminHT}% test empy node appearance
    \nodebox{}{.7\textwidth}{\nodeboxminHT}% test empy node appearance
    \nodebox{}{.2\textwidth}{\nodeboxminHT}% test empy node appearance
  \end{formrow}
}
\makeatother % simulate end of package code

\ABCMANUFACTURERCONTACTINFORMATION{Highly-specialized Special Ops}

\begin{document}
\makeform
\end{document}

在此处输入图片描述

相关内容