这里我使用了三个宏,,\a
它们被捕获到中。我测量了的高度,并将其结果设置为高度寄存器。\b
\c
\BODY
\BODY
\nodeboxht
这确保所有盒子的高度与最高盒子的高度相匹配。
测量的高度\BODY
有负面影响。如果\BODY
包含计数器,它将在测量步骤中递增,然后在排版步骤中再次递增。这会产生不想要的偶数。我可能可以用一些\ifnum
逻辑来解决这个问题,但我想知道是否有更好的方法。
输出
计数器用 0 初始化。数字 1、3 和 5 是不可见的,因为它们在第一次\BODY
调用时出于测量目的进行了扩展。
我的解决方案
使用全局 TeX 寄存器(LaTeX 计数器),我可以将其用作 TRUE / FALSE 指示器。这完全是黑客行为。
\newcount\counterTF% TeX Register init
\global\counterTF=0\relax% TeX Counter Definition Equivalent of LaTeX
% Redefine with logic such that it only steps and uses counter every other call
\gdef\typesetcounter{\ifnum0=\counterTF\relax\global\counterTF=1\else\stepcounter{boxcount}\arabic{boxcount}\global\counterTF=0\fi}
代码
\documentclass{article}
\usepackage{fontspec}% xelatex
\usepackage{environ}
\usepackage{tikz}
% Plain TeX Box for Reference
%\newbox\mybox
%\setbox\mybox=\hbox{Hello}
%\showthe\wd\mybox
%\bye
% LaTeX Wrapper for TeX Box for Reference
%\newsavebox{\mybox}
%\savebox{\mybox}{\parbox{4cm}{\lorem}}
%\showthe\wd\mybox
\makeatletter % make @ cat code 11
% Initialize Registers
\newcounter{boxcount}
\setcounter{boxcount}{0}
\gdef\typesetcounter{\stepcounter{boxcount}\arabic{boxcount}}
% Define Row Environment (using environ package for \BODY)
\NewEnviron{row}{%
\par\noindent\ignorespaces%
\sbox\@tempboxa{\BODY}% save contents of environment (contains all boxes)
\nodeboxht=\the\ht\@tempboxa% measure height of \BODY, which implies box with max height
\BODY% typeset body
\ignorespacesafterend%
}
% Define Node Box Formatting
\long\def\nodebox#1#2#3{\tikz \node [outer sep=0pt,draw, minimum width=#1,minimum height=#2, align=center] {#3};}
% Initialize Node Height Dimension
\newdimen\nodeboxht
\nodeboxht=\dimexpr2\fontcharht\font`|\relax% set to height of a strut
% Define Node Boxes to Fill Row
\def\a{\nodebox{2cm}{\nodeboxht}{\typesetcounter.\quad abc}}
\def\b{\nodebox{3cm}{\nodeboxht}{def}}
\def\c{\nodebox{1cm}{\nodeboxht}{ghi\\anotherline\\anotherline}}
\makeatother % make @ cat code 9
\begin{document}
\begin{row}% Desired Row Number: 1
\a{}%
\b{}%
\c{}%
\end{row}
\begin{row}% Desired Row Number: 2
\a{}%
\b{}%
\c{}%
\end{row}
\begin{row}% Desired Row Number: 3
\a{}%
\b{}%
\c{}%
\end{row}
\end{document}
有关的
答案1
这里,我在执行 之前重新定义\typesetcounter
为(仅用于垂直测量)。设置 之后,我重新定义为您想要的值(增加并排版计数器)。\relax
\sbox
\sbox
\typesetcounter
\documentclass{article}
%\usepackage{fontspec}% xelatex
\usepackage{environ}
\usepackage{tikz}
% Plain TeX Box for Reference
%\newbox\mybox
%\setbox\mybox=\hbox{Hello}
%\showthe\wd\mybox
%\bye
% LaTeX Wrapper for TeX Box for Reference
%\newsavebox{\mybox}
%\savebox{\mybox}{\parbox{4cm}{\lorem}}
%\showthe\wd\mybox
\makeatletter % make @ cat code 11
% Initialize Registers
\newcounter{boxcount}
\setcounter{boxcount}{0}
% Define Row Environment (using environ package for \BODY)
\NewEnviron{row}{%
\par\noindent\ignorespaces%
\let\typesetcounter\relax
\sbox\@tempboxa{\BODY}% save contents of environment (contains all boxes)
\gdef\typesetcounter{\stepcounter{boxcount}\arabic{boxcount}}
\nodeboxht=\the\ht\@tempboxa% measure height of \BODY, which implies box with max height
\BODY% typeset body
\ignorespacesafterend%
}
% Define Node Box Formatting
\long\def\nodebox#1#2#3{\tikz \node [outer sep=0pt,draw, minimum width=#1,minimum height=#2, align=center] {#3};}
% Initialize Node Height Dimension
\newdimen\nodeboxht
\nodeboxht=\dimexpr2\fontcharht\font`|\relax% set to height of a strut
% Define Node Boxes to Fill Row
\def\a{\nodebox{2cm}{\nodeboxht}{\typesetcounter.\quad abc}}
\def\b{\nodebox{3cm}{\nodeboxht}{def}}
\def\c{\nodebox{1cm}{\nodeboxht}{ghi\\anotherline\\anotherline}}
\makeatother % make @ cat code 9
\begin{document}
\begin{row}% Desired Row Number: 1
\a{}%
\b{}%
\c{}%
\end{row}
\begin{row}% Desired Row Number: 2
\a{}%
\b{}%
\c{}%
\end{row}
\begin{row}% Desired Row Number: 3
\a{}%
\b{}%
\c{}%
\end{row}
\end{document}