# Centralizing tikzpicture environment “inside” a minipage, both horizontally and vertically

Honestly, I do not know if tex.stackexchange.com already exists a similar or exact answer to this question but I'll leave it here and if there is any solution already posted I'm waiting to be notified, grateful. I have found a solution that for me I would classify as an alternative because of the fact that I can not understand why it worked. So I am posting this question below so I even respond to those who also need to be able to find it easily. So my solution below will already serve as MWE for this very issue.

I do not know exactly what caused me to get it but I did the following path:

Attempts to find solutions:

It did not work "but it helped to get the solution", grateful to them too: https://tex.stackexchange.com/a/446763/178949 and https://tex.stackexchange.com/a/446853/178949.

It worked (I am grateful to @Alexey Malistov) I needed to delete or not use the following commands: "\ vbox", "\ vsize to": https://tex.stackexchange.com/a/62493/178949.

I've tried to use \ begin {center} \ end {center}, or use \ vfill and \ vspace * {\ fill} along with \ hfill and \ hspace * {\ fil}, or just \ vfill along with \ hfill and still combined simultaneously or not with \ centering, but always the grid of lines ends up going out of the box \ fbox. Using the whole solution, without changing anything, from @Alexey Malistov as quoted above, I got an approximation of the exact centering horizontally and vertically but continued to put the grid (or the tikz environment) far down. Then I guessed that this extra offset below the vertical and horizontal center of the minipage was happening by the use of \ vbox. So I deleted \ vbox, and got to an exact vertical centering. So I combined, encapsulating everything before, with \ hfil.

If I do

{\ hfil {{\ vfil {
\ begin {tikzpicture}
... command to draw the grid ...
\ end {tikzpicture}
} \ vfill} \ hfil}}


I do not know why but it did not work. But using \ vfil and \ hfil in the order of the code below worked. I appreciate comments that help me understand why it worked.

I have taken a capture of Zathura PDF and soon perceive the accuracy of the position in relation to the whole page of the document and simultaneously the centralization of the drawing of the grid inside the minipage represented by the edges of the box of the command \fbox.

\documentclass{article}
\usepackage{fancybox,tikz}
\usepackage[paperwidth=35cm,paperheight=35cm,bottom=2cm,left=2cm]{geometry}
\thispagestyle{empty}
\begin{document}
\fbox{
\begin{minipage}[t][25cm][t]{\linewidth}%
{\vfil{
{\hfil{\begin{tikzpicture}
\draw [draw, black,help lines] (-4,-1.5) grid  (5,9.6);
\end{tikzpicture}

}\hfil}\vfil}}

\end{minipage}
}
\end{document}


This answer makes the grid and saves it in a box (\mygrid), using \savebox.

\savebox{\mygrid}{\tikz{\draw[step=2.5mm] (-4,-1.5)  grid (5,9.6);}}


Saving the grid in a box, is equivalent to the OP's use of a minipage to get the grid into a box.

mygrid is placed in the middle of the page, the location of which is defined by a pair of orthogonal coordinates: (current page text area.north) and (current page text area.east). These page coordinates are created by loading the package tikzpagenodes.

The intersection of these coordinates is defined using |-. See the very good explanation here: TikZ: What EXACTLY does the the |- notation for arrows do? for the use of the |- syntax.

\node (center) at (current page text area.north |- current page text area.east) {\usebox{\mygrid}};


We can show this placement by adding some markers for the page coordinates and guides. First, a marker (coordinate ctpaN) is placed at (current page text area.north) using:

\node[circle,fill=blue,radius=4pt,label=above:{\Large current page text area.north}] (ctpaN) at (current page text area.north) {};


and a second marker (coordinate ctpaE) is placed at (current page text area.east) using

\node[circle,fill=blue,radius=4pt,label=above left:{\Large current page text area.east}]  (ctpaE) at (current page text area.east) {};


We then draw a line connecting the coordinates labelled (ctpaN) and (ctpaE). Again, we make use of the |- syntax to the draw line.

\draw[blue,thick] (ctpaN) |- (ctpaE);


The use of page coordinates can be adapted to placing content anywhere on a page. A second example is provided that uses partway calculations (TikZ manual section 4.2.1) to place example-image-a in the lower left section of the page.

This is a more generalized way of placing content compared to using \hfil and \vfil. Instead, we can precisely place content anywhere on a page, including in the header, footer and margins.

Another option for specific placement of content is using the textpos package.

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage[showframe,paperwidth=35cm,paperheight=35cm,bottom=2cm,left=2cm]{geometry} % added showframe to show the margins
\usepackage{tikzpagenodes}
\thispagestyle{empty}
\newsavebox{\mygrid}
\savebox{\mygrid}{\tikz{\draw[step=2.5mm] (-4,-1.5) grid (5,9.6);}}
\begin{document}
\begin{tikzpicture}[remember picture,overlay]
% Place the grid
\node[circle,fill=blue,radius=4pt,label=above:{\Large current page text area.north}] (ctpaN) at (current page text area.north) {};
\node[circle,fill=blue,radius=4pt,label=above left:{\Large current page text area.east}]  (ctpaE) at (current page text area.east) {};
\draw[blue,thick] (ctpaN) |- (ctpaE);
\node (center) at (current page text area.north |- current page text area.east) {\usebox{\mygrid}};
% Place example-image-a uisng partway calculations. Load usetikzlibrary{calc}
\node[circle,fill=red,radius=4pt] (ul) at ($(current page text area.north west)!0.25!(current page text area.north)$) {};
\node[circle,fill=red,radius=4pt] (ll) at ($(current page text area.west)!0.75!(current page text area.south west)$) {};
\draw[red,thick] (ul) |- (ll);
\node (ull) at (ll -| ul) {\includegraphics[width=1.5cm]{example-image-a}};
\end{tikzpicture}
\end{document}


• Thanks for your reply Ross. It will help me understand the dynamics (interaction) between centering tools, drawing environments (eg: \ tikzpicture) and the resulting behavior within and minipage. Although you have not used minipage in your answer, I did the test and it really works. – Yaacov NNNNM Jul 18 '19 at 19:15
• Diego. Hopefully it's clearer now. – Ross Jul 19 '19 at 2:29