tikz 中不需要的额外垂直间距:这是一个错误吗?

tikz 中不需要的额外垂直间距:这是一个错误吗?

我正在编写一个环境,用于将其内容格式化为带有背景的框,并且可以跨页面拆分。我使用的拆分算法类似于boites包裹。

我想使用 填充背景(并执行其他操作,例如绘制带圆角的框架)tikz。但某些线条之间插入了一些不必要的垂直间距。当我使用\colorbox填充背景时,不会出现此间距。

这是一个显示该问题的最小工作示例:

\documentclass{article}
\usepackage[paperwidth=140mm,paperheight=55mm,margin=5mm]{geometry}
\usepackage{tikz}
\usepackage{multicol}
\pagestyle{plain}

\makeatletter

% The purpose of the `brbox` environment below is to typeset a box with
% a given background colour. The box may be split by page breaks.
% 
% The algorithm used is similar to the one found in the `boites`
% package:
% 
% - first the text is formatted in a vbox
%
% - then each line of the vbox is extracted from the vbox and output in
%   a colorbox.

\newbox\boxhead
\newbox\boxtail
\newbox\boxtemp
\newdimen\dimtemp
\newif\ifbrbox@continue

% split \boxtail between the first line (to be stored in \boxhead) and
% the rest (to be stored in \boxtail);
\def\@boxsplit{%
  % total height (height + depth) of original box
  \dimtemp\ht\boxtail
  \advance\dimtemp\dp\boxtail
  % split original box in order to retrieve its first line
  \setbox\boxhead\vsplit\boxtail to \z@
  % the height and depth of the first box are zero: fix them
  \setbox\boxhead\vbox{\unvbox\boxhead}%
  \wd\boxhead\linewidth%?????????????????????????????????????????????????
  \ifvoid\boxtail
  \else
    % the gap (interline space) between first line and the rest has been
    % lost; restore original size: the difference of the original total
    % height and the total height of each box
    \setbox\boxtemp\vbox{\copy\boxhead\copy\boxtail}%
    \advance\dimtemp-\ht\boxtemp
    \advance\dimtemp-\dp\boxtemp
    \advance\dimtemp\dp\boxhead
    \dp\boxhead\dimtemp
  \fi
}

% add \fboxsep to the last line
\def\brbox@addfsepdp{%
  \dimtemp\dp\boxhead
  \advance\dimtemp\fboxsep
  \dp\boxhead\dimtemp
}

\newenvironment{brbox}{%
  \setbox\boxtail\vbox\bgroup
}{%
  \egroup
  {%
    \noindent
    \splittopskip\z@ % glue inserted at the top of a box resulting from a \vsplit
    \baselineskip\z@
    \lineskiplimit\z@
    \lineskip\z@
    \vfuzz\maxdimen
    \@boxsplit
    \setbox\boxhead\vbox{\vskip\fboxsep\box\boxhead}% add \fboxsep to the first line
    \leavevmode
    \ifvoid\boxtail
      \brbox@addfsepdp % add \fboxsep to the last line
      \brbox@singleline
    \else
      \brbox@firstline
      \hfil
      \brbox@continuetrue
      \loop
        \@boxsplit
        \leavevmode
        \ifvoid\boxtail
          \brbox@addfsepdp % add \fboxsep to the last line
          \brbox@lastline
          \brbox@continuefalse
        \else
          \brbox@middleline
          \hfil
        \fi
        \ifbrbox@continue\repeat
    \fi
  }%
}


\def\brbox@tikzline{\tikz\node[inner xsep=0pt,inner ysep=0pt,fill=yellow!30](p){\copy\boxhead};}
\def\tikzlines{%
  \let\brbox@singleline \brbox@tikzline
  \let\brbox@firstline  \brbox@tikzline
  \let\brbox@middleline \brbox@tikzline
  \let\brbox@lastline   \brbox@tikzline
}
\tikzlines

% alternative: use colorbox to fill the background of the box
\def\brbox@bgline{{\fboxsep\z@ \colorbox{yellow!35}{\box\boxhead}}}

\def\bglines{%
  \let\brbox@singleline \brbox@bgline
  \let\brbox@firstline  \brbox@bgline
  \let\brbox@middleline \brbox@bgline
  \let\brbox@lastline   \brbox@bgline
}

\makeatother


\newcommand{\mytext}{%
  \LaTeX\ is a high-quality typesetting system; it includes features
  designed for the production of technical and scientific
  documentation.
  \LaTeX\ is the de facto standard for the communication and publication
  of scientific documents.
}

\begin{document}

\begin{multicols}{3}
  \mytext

  \columnbreak
  \begin{brbox} \mytext \end{brbox}

  \columnbreak
  \bglines
  \begin{brbox} \mytext \end{brbox}
\end{multicols}

\end{document}

在此处输入图片描述

如何消除仅在tikz使用时出现的额外垂直间距?

这看起来像是一个tikz错误。

答案1

您缺少\par(以及设置\parindent为零)。我还设置了\hbadness以避免虚假警告。

\documentclass{article}
\usepackage[paperwidth=140mm,paperheight=55mm,margin=5mm]{geometry}
\usepackage{tikz}
\usepackage{multicol}
\pagestyle{plain}

\makeatletter

% The purpose of the `brbox` environment below is to typeset a box with
% a given background colour. The box may be split by page breaks.
% 
% The algorithm used is similar to the one found in the `boites`
% package:
% 
% - first the text is formatted in a vbox
%
% - then each line of the vbox is extracted from the vbox and output in
%   a colorbox.

\newbox\boxhead
\newbox\boxtail
\newbox\boxtemp
\newdimen\dimtemp
\newif\ifbrbox@continue

% split \boxtail between the first line (to be stored in \boxhead) and
% the rest (to be stored in \boxtail);
\def\@boxsplit{%
  % total height (height + depth) of original box
  \dimtemp\ht\boxtail
  \advance\dimtemp\dp\boxtail
  % split original box in order to retrieve its first line
  \setbox\boxhead\vsplit\boxtail to \z@
  % the height and depth of the first box are zero: fix them
  \setbox\boxhead\vbox{\unvbox\boxhead}%
  \wd\boxhead\linewidth%?????????????????????????????????????????????????
  \ifvoid\boxtail
  \else
    % the gap (interline space) between first line and the rest has been
    % lost; restore original size: the difference of the original total
    % height and the total height of each box
    \setbox\boxtemp\vbox{\copy\boxhead\copy\boxtail}%
    \advance\dimtemp-\ht\boxtemp
    \advance\dimtemp-\dp\boxtemp
    \advance\dimtemp\dp\boxhead
    \dp\boxhead\dimtemp
  \fi
}

% add \fboxsep to the last line
\def\brbox@addfsepdp{%
  \dimtemp\dp\boxhead
  \advance\dimtemp\fboxsep
  \dp\boxhead\dimtemp
}

\newenvironment{brbox}{%
  \setbox\boxtail\vbox\bgroup
}{%
  \egroup
  {%
    \parindent0pt     %<--- added
    \hbadness=10000   %<--- added
    \splittopskip\z@ % glue inserted at the top of a box resulting from a \vsplit
    \baselineskip\z@
    \lineskiplimit\z@
    \lineskip\z@
    \vfuzz\maxdimen
    \@boxsplit
    \setbox\boxhead\vbox{\vskip\fboxsep\box\boxhead}% add \fboxsep to the first line
    \leavevmode
    \ifvoid\boxtail
      \brbox@addfsepdp % add \fboxsep to the last line
      \brbox@singleline
    \else
      \brbox@firstline
      \hfil
      \brbox@continuetrue
      \loop
        \@boxsplit
        \leavevmode
        \ifvoid\boxtail
          \brbox@addfsepdp % add \fboxsep to the last line
          \brbox@lastline
          \brbox@continuefalse
        \else
          \brbox@middleline
          \hfil
        \fi
        \ifbrbox@continue\repeat
    \fi
    \par %<--- added
  }%
}


\def\brbox@tikzline{%
  \tikz\node[inner xsep=0pt,inner ysep=0pt,fill=yellow!30](p){\copy\boxhead};\hfill
}
\def\tikzlines{%
  \let\brbox@singleline \brbox@tikzline
  \let\brbox@firstline  \brbox@tikzline
  \let\brbox@middleline \brbox@tikzline
  \let\brbox@lastline   \brbox@tikzline
}
\tikzlines

% alternative: use colorbox to fill the background of the box
\def\brbox@bgline{{\fboxsep\z@ \colorbox{yellow!35}{\box\boxhead}}}

\def\bglines{%
  \let\brbox@singleline \brbox@bgline
  \let\brbox@firstline  \brbox@bgline
  \let\brbox@middleline \brbox@bgline
  \let\brbox@lastline   \brbox@bgline
}

\makeatother


\newcommand{\mytext}{%
  \LaTeX\ is a high-quality typesetting system; it includes features
  designed for the production of technical and scientific
  documentation.
  \LaTeX\ is the de facto standard for the communication and publication
  of scientific documents.
}

\begin{document}

\begin{multicols}{3}
  \mytext

  \columnbreak

  \begin{brbox} \mytext \end{brbox}

  \columnbreak

  \bglines
  \begin{brbox} \mytext \end{brbox}

\end{multicols}

\end{document}

我用 标记了更改<--- added

在此处输入图片描述

相关内容