tcolorbox vs. TikZ vs. geometry: How to put a proper grid below a tcolorbox

On a DINA4 (210 × 297 [mm]) paper I want to create the following:

• I need ca. 2cm margins to the left and the right; in the example the vertical margins becomes 2.5cm.
• I need a tcolorbox with a total height of 6cm.
• Below of that tcolorbox I want a grid of 2 × 2 [cm] with as much as possible squares in the width.

So I choose

 \usepackage[    width=16cm,    height=26cm,    ]{geometry}


that should allow me 8 grid-squares in the width.

But why does my grid

% Grid:
\begin{tikzpicture}[every path/.style=red,
remember picture,overlay,]
\draw[step=2, shift={(Textbox)}] (0,0) grid +(\linewidth-0.0*\pgflinewidth,-8);
\end{tikzpicture}


becomes a bad look at its corners and does not match correct on the tcolorbox (even if I choose line cap=rect)? What do I have to do?

BTW: Would it be better to create the grid as a tcolorbox too?

\documentclass[a4paper]{article} % DINA4 (210 × 297 [mm])
\usepackage[%showframe=true,
width=16cm,
height=26cm,
]{geometry}
\pagestyle{empty}

\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage[most]{tcolorbox}

\begin{document}
\begin{tcolorbox}[
height=6cm,
sharp corners,
enhanced, remember, finish={%
\draw[blue,ultra thick,latex-] (frame.south west) coordinate[label=135:X](Textbox)-- +(3,3) node[right]{Here to put on the grid.}; }
]
A text.
\end{tcolorbox}

% Grid:
\begin{tikzpicture}[every path/.style=red,
remember picture,overlay,]
\draw[step=2, shift={(Textbox)}] (0,0) coordinate(X) grid +(\linewidth-0.0*\pgflinewidth,-8);
\end{tikzpicture}
\end{document}

• The width must be equal to 17cm and not 16 cm because 21cm - 4 cm = 17 cm. – AndréC Jul 14 at 19:19
• Tikz option line cap=rect should fix the problem. – muzimuzhi Z Jul 14 at 19:22
• @AndréC But with 'width=17cm' I have a problem with the 2cm squares.... – cis Jul 14 at 19:31
• @muzimuzhiZ I see. That solves the problem with the bad look, but then the grid does not match correctly under the the tcolorbox. – cis Jul 14 at 19:31
• ca. = circa = approximately. dict.leo.org/englisch-deutsch/circa – cis Jul 14 at 21:09

There are differences between how the width of a tcolorbox environment and a \draw grid are determined.

• In a tcolorbox environment, its total natural width is exactly \linewidth. Here the entire left and right rules are drawn within that width.
• In a \draw (0, 0) grid (1, 1);, its total width is "1cm + line width", because tikz draws a line with its center right along the path.

In you situation,

• Since you need complete grids drawn, there is no room to change \draw[..., line cap=rect] (0, 0) grid +(\linewidth, -8).
• Then, the tcolorbox environment is narrower than the grid by half of grid line width, both at left and at right sides. This is what you pointed out in question comment.

Here is an attempt, though not elegant enough:

• Enlarge the tcolorbox by half the line width of grid, both at left and at right. Now tcolorbox has total width of "\linewidth + grid line width", which is the same as the following grid.
• Shift the grid horizontally by half of grid line width.
\documentclass[a4paper]{article} % DINA4 (210 × 297 [mm])
\usepackage[%showframe=true,
width=16cm,
height=26cm,
]{geometry}
\pagestyle{empty}

\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage[most]{tcolorbox}

\begin{document}
\newlength{\gridlinewidth}
\setlength{\gridlinewidth}{.4pt} % default tikz line width

\begin{tcolorbox}[
height=6cm,
sharp corners,
boxrule=10pt,
grow sidewards by=.5\gridlinewidth,
enhanced,
remember,
finish={%
\draw[blue,ultra thick,latex-]
(frame.south west) coordinate[label=135:X] (Textbox) -- +(3,3)
node[right] {Here to put on the grid.};
}
]
A text.
\end{tcolorbox}

% Grid:
\begin{tikzpicture}[
every path/.style=red,
remember picture, overlay
]
\draw[step=2, shift={($(Textbox) + (.5\gridlinewidth, 0)$)}, line cap=rect]
(0, 0) grid +(\linewidth, -8);
\end{tikzpicture}
\end{document}


left side

right side

• Strange, I did not think that was such a big problem. Whatever, the best (and most unproblematic) result I get when I draw the grid in a new tcolorbox. See my answer. – cis Jul 14 at 22:35

Basing on the answer of muzimuzhi Z, I suggest a variant with a single box which integrates drawing the grid:

\documentclass[a4paper]{article} % DINA4 (210 × 297 [mm])
\usepackage[%showframe=true,
width=16cm,
height=26cm,
]{geometry}
\pagestyle{empty}

\usepackage{tikz}
\usepackage[most]{tcolorbox}

\begin{document}
\newlength{\gridlinewidth}
\setlength{\gridlinewidth}{.4pt} % default tikz line width

\begin{tcolorbox}[
enhanced,
height=6cm,
sharp corners,
boxrule=10pt,
grow sidewards by=.5\gridlinewidth,
enlarge bottom finally by=8cm+\gridlinewidth,
finish={%
\begin{scope}[shift={([xshift=0.5\gridlinewidth, yshift=-0.5\gridlinewidth]frame.south west)}]
\draw[step=2, line cap=rect, red, line width=\gridlinewidth]
grid +(\linewidth, -8);
\end{scope}
}
]
A text.
\end{tcolorbox}

Text after box and grid.

\end{document}


If the height of the grid should be other than 8cm (4 squares), one has to adapt enlarge bottom finally by=8cm+\gridlinewidth, and grid +(\linewidth, -8); accordingly.

If your page contains nothing after box and grid, you can omit enlarge bottom finally by=8cm+\gridlinewidth, completely.

• Seems, that the solution flys around me ears, if I set \setlength{\gridlinewidth}{7.4pt} [means larger that 0.4pt]. So, I would say that the solution only works approximately, not in general. How can I fix that? I ask because the content of this tcbox is complex (more boxes ...) – cis Jul 22 at 17:30
• @cis If you set \gridlinewidth to 7.4pt, you must use line width=7.4pt or line width=\gridlinewidth in the grid drawing options, too. – esdd Jul 22 at 21:58
• Stragne, I Thought I had used that. Whatever. Very well. – cis Jul 23 at 0:47
• I added a line width=\gridlinewidth to my answer to avoid confusions. – Thomas F. Sturm Jul 23 at 7:59

With grid inside a new tcolorbox:

\documentclass[a4paper]{article} % DINA4 (210 × 297 [mm])
\usepackage[%showframe=true,
width=16cm,
height=26cm,
]{geometry}
\pagestyle{empty}

\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage[most]{tcolorbox}

\begin{document}
\begin{tcolorbox}[
height=6cm,
sharp corners,
after skip=0pt,
enhanced, remember, finish={%
\draw[blue,ultra thick,latex-] (frame.south west) coordinate[label=135:X](Textbox)-- +(3,3) node[right]{Here to put on the grid.}; },
]
A text.
\end{tcolorbox}

% Grid - old
%\begin{tikzpicture}[every path/.style=red,
%remember picture,overlay, %line cap=rect
%]
%\draw[step=2, shift={($(Textbox)+(0.5*\pgflinewidth,0)$)}] (0,0) coordinate(X) grid +(16cm-0.5*\pgflinewidth,-8);
%\end{tikzpicture}

% Grid - new
\begin{tcolorbox}[
height=8cm,
boxrule=0pt, %frame empty,
sharp corners,
colback=white,
before skip=0pt, after skip=0pt,
%
enhanced,
underlay={\begin{tcbclipinterior}
\draw[step=2, red, shift={(interior.north west)}
] (interior.south west) grid (interior.north east);
\end{tcbclipinterior}},
]
\end{tcolorbox}

\end{document}

• This grid is correct or better say it adjusts to previous tcolorbox, because container tcolorbox cuts the inner grid on their sides. If you look carefully, you will see that left and right grid lines are narrower than inner lines. If you want to see it more evidently, draw the grid with line width=2mm or any other very thick line. – Ignasi Jul 15 at 6:45