TransWikia.com

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

TeX - LaTeX Asked on August 27, 2021

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?

enter image description here

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}

3 Answers

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=.5gridlinewidth,
  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) + (.5gridlinewidth, 0)$)}, line cap=rect] 
    (0, 0) grid +(linewidth, -8);
end{tikzpicture}
end{document}

left side enter image description here

right side enter image description here

Answered by muzimuzhi Z on August 27, 2021

With grid inside a new tcolorbox:

enter image description here

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}

Answered by cis on August 27, 2021

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=.5gridlinewidth,
  enlarge bottom finally by=8cm+gridlinewidth,
  finish={%
    begin{scope}[shift={([xshift=0.5gridlinewidth, yshift=-0.5gridlinewidth]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}

enter image description here

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.

Answered by Thomas F. Sturm on August 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP