[Tex/LaTex] Supress space introduced by drawing borders of nodes with tikz

spacingtikz-pgf

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}

with spaces

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.

without spaces

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 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:

Sample output

\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}