When borders around a rectangular node is drawn, some space is also added, as can be seen by the example below.
\documentclass{article}
\usepackage[papersize={70mm,34mm}]{geometry}
\usepackage{tikz}
\tikzset{
boxdraw/.style={very thick,rounded corners,fill=yellow!30, },
}
\newcommand{\firstline}[1]{%
\noindent
\begin{tikzpicture}
\node[text width=\linewidth](p){#1};
\draw[boxdraw] (p.south west) -- (p.north west) -- (p.north east) -- (p.south east);
\node[text width=\linewidth]{#1};
\end{tikzpicture}%
\newline
}
\newcommand{\middleline}[1]{%
\noindent
\begin{tikzpicture}
\node[text width=\linewidth,fill=yellow!30](p){#1};
\draw[boxdraw] (p.north west) -- (p.south west) (p.north east) -- (p.south east);
\end{tikzpicture}%
\newline
}
\newcommand{\lastline}[1]{%
\noindent
\begin{tikzpicture}
\node[text width=\linewidth](p){#1};
\draw[boxdraw] (p.north west) -- (p.south west) -- (p.south east) -- (p.north east);
\node[text width=\linewidth](p){#1};
\end{tikzpicture}%
}
\splittopskip 0pt
\baselineskip 0pt
\lineskiplimit 0pt
\lineskip 0pt
\begin{document}
\firstline{This is the first line}
\middleline{and this is my second line}
\middleline{followed by another middle line}
\lastline{followed by this one last line.}
\end{document}
Look at the space between the lines.
If the line
\draw[boxdraw] (p.north west) -- (p.south west) (p.north east) -- (p.south east);
(which draws the border around the rectangle node) is commented out in the definition of \middleline
, the space between middle lines are not present.
How can these spaces be completely removed while keeping the borders?
EDITED:
A brief explanation of the purpose of these macros
I am trying to write a package for boxes that may break across pages. I know there are already some packages for that (like mdframed
and tcolorbox
), but most of them fail to break inner boxes in nested boxes. With nested boxes, only the outermost box can be breakable. That is due to the algorithm they implement for breaking boxes. I want to overcome this issue by using a different splitting algorithm, based on the one employed by the boites
package.
The algorithm I am going to use first gets the contents of the breakable box into a vbox
. Then the vbox
is split into its individual lines. Then each line receives a background colour and a frame.
The macros \firstline
, \middleline
and \lastline
would be used to give a background colour and a frame to each of those lines. Because of framing, I will need one macro for the first line of the breakable box, one for middle lines, and one for the last line.
Best Answer
What you are seeing is a combination of the
outer sep
of the node and the extra height and depth added by the border curve at its start and end points. By defaultouter sep
is.5\pgflinewidth
. For stylevery thick
this curve is1.2pt
wide. So to correct for this you should setouter sep
to-.6pt
.Doing that you will see that the very slight over run of the yellow rectangle at the round corners becomes more apparent. You should therefore fill the top and bottom boxes carefully according to the correct path.
Also you will notice the bordes still don't join up, this is due to
line cap
being set tobutt
as standard, you need it to berect
: