# Supress space introduced by drawing borders of nodes with tikz

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.

• You need to specify the option outer sep. – Marco Daniel Oct 27 '13 at 11:21
• @MarcoDaniel The option outer sep=0pt does not help. Setting it to a negative value may suppress the space, but the borders are also reduced, what is not acceptable. – Romildo Oct 27 '13 at 11:50
• @Romildo By the way, why this solution instead of simply writing \node[draw, very thick, rounded corners, fill=yellow!30, text width=\linewidth] {This is the first line\\and this is my second line\\followed by another middle line\\followed by this one last line.};? – Alenanno Oct 27 '13 at 12:20
• @Alenanno I have edited the question to explain the purpose of these macros, which answers your question. – Romildo Oct 27 '13 at 13:01

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 default outer sep is .5\pgflinewidth. For style very thick this curve is 1.2pt wide. So to correct for this you should set outer 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 to butt as standard, you need it to be rect:

\documentclass{article}

\usepackage[papersize={70mm,34mm}]{geometry}
\usepackage{tikz}

\tikzset{
boxdraw/.style={very thick,rounded corners,line cap=rect}
}

\newcommand{\firstline}[1]{%
\noindent
\begin{tikzpicture}
\node[text width=\linewidth,outer sep=-.6pt](p){#1};
\filldraw[very thick,yellow!30] (p.south west) [rounded corners] -- (p.north
west) -- (p.north east) [sharp corners] -- (p.south east) -- cycle;
\draw[boxdraw] (p.south west) -- (p.north west) -- (p.north east)
-- (p.south east);
\node[text width=\linewidth,outer sep=-.6pt] at (p) {#1};
\end{tikzpicture}%
\newline
}

\newcommand{\middleline}[1]{%
\noindent
\begin{tikzpicture}
\node[text width=\linewidth,outer sep=-.6pt,fill=yellow!30](p){#1};
\draw[very thick,yellow!30] (p.north west) -- (p.north east);
\draw[very thick,yellow!30] (p.south west) -- (p.south east);
\draw[boxdraw] (p.north west) -- (p.south west);
\draw[boxdraw] (p.north east) -- (p.south east);
\end{tikzpicture}%
\newline
}

\newcommand{\lastline}[1]{%
\noindent
\begin{tikzpicture}
\node[text width=\linewidth,outer sep=-.6pt](p){#1};
\filldraw[very thick,yellow!30] (p.north west) [rounded corners] -- (p.south
west) -- (p.south east) [sharp corners] -- (p.north east) -- cycle;
\draw[boxdraw] (p.north west) -- (p.south west) -- (p.south east)
-- (p.north east);
\node[text width=\linewidth,outer sep=-.6pt] at (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}


A quick fix would be to replace the command \lineskip 0pt with \lineskip -2pt.

• Was the space to skip in \lineskip -2pt found by trial and error? – Romildo Oct 27 '13 at 11:55
• @Romildo More or less, -1pt wasn't enough, but I think that -2 is exactly the space. I can try to look for a cleaner solution though. :P – Alenanno Oct 27 '13 at 12:00
• I will need to know this space exactly, because I am going to use these macros to typeset boxes that may break across pages. The space between lines in the breakable box will have to be identical to the space when the contents is typeset without the breakable box. – Romildo Oct 27 '13 at 12:01