3

My question is essentially the same as this question, but I'm asking again because the answer provided is not very resilient. In particular, it doesn't work for the kind of diagram I want.

I want to use Tikz to draw a diagram with lots of nodes and arrows; essentially, a flowchart. The diagram should start with a single node which is centered on the page (in the MWE below, this is the "Hello there" box). If I try to just use the \centering command, the whole picture gets centered, which is not what I want.

The answer I linked above suggests using the \useasboundingbox command. However, this doesn't seem to do what I want. Here is my minimal working example:

\documentclass[a4paper, 11pt]{article}
\usepackage{tikz}
\begin{document}

This is a line of text which has been written with no purpose
other than to help illustrate how the figure below appears on a page.

\begin{figure}[ht]
\centering
\begin{tikzpicture}[rect/.style={rectangle, draw=black, thin}]

\node[rect] (box1) at (0,0) {Hello there};
% Location 1
\node[rect] (box2) at (5,-1) {Hi};
% Location 2

\end{tikzpicture}
\end{figure}

This is a line of text which has been written with no purpose
other than to help illustrate how the figure above appears on a page.
\end{document}

The linked answer suggests using the command \useasboundingbox (box1.south east) rectangle (box1.north west);. If I put this command at location 2, then the bounding box includes both boxes "Hello there" and "Hi", and the box "Hello there" is not centered (see below). Illustration of when command is placed at location 2

If I put this command at location 1, then the bounding box only includes the "Hello there" box; this centers it the way I want, but then the box "Hi" ends up overlapping with the text below (see below). Illustration of when command is placed at location 1

How can I make the "Hello there" box be centered on the page?

1
  • use location 2. \useasboundingbox (box1.south east) rectangle (box1.west |- box2.south); Commented 18 hours ago

3 Answers 3

2

Try using trim left and trim right instead of \useasboundingbox. The idea is to “trim” the horizontal bounding box so that, from TeX’s point of view, the width of the picture is just the width of the node box1, while the vertical size is still determined by the entire diagram.

\documentclass[a4paper, 11pt]{article}
\usepackage{tikz}

\begin{document}

This is a line of text which has been written with no purpose
other than to help illustrate how the figure below appears on a page.

\begin{figure}[ht]
\centering
\begin{tikzpicture}[
  rect/.style={rectangle, draw=black, thin},
  trim left=(box1.west),
  trim right=(box1.east)
]

\node[rect] (box1) at (0,0) {Hello there};
\node[rect] (box2) at (5,-1) {Hi};

\end{tikzpicture}
\end{figure}

This is a line of text which has been written with no purpose
other than to help illustrate how the figure above appears on a page.

\end{document}

enter image description here

1
% Source - https://tex.stackexchange.com/q/762400
% Posted by Sambo
% Retrieved 2026-04-30, License - CC BY-SA 4.0

\documentclass[a4paper, 11pt]{article}
\usepackage{tikz}
\begin{document}

This is a line of text which has been written with no purpose
other than to help illustrate how the figure below appears on a page.

\begin{figure}[ht]
\centering
\begin{tikzpicture}[rect/.style={rectangle, draw=black, thin}]

\node[rect] (box1) at (0,0) {Hello there};
% Location 1
\node[rect] (box2) at (5,-1) {Hi};
% Location 2
\pgfresetboundingbox
\useasboundingbox (box1.north west) rectangle (box1.east |- box2.south);

\end{tikzpicture}
\end{figure}

This is a line of text which has been written with no purpose
other than to help illustrate how the figure above appears on a page.
\end{document}

centred on first box

1

To make (0,0) the center, add a point to the left an equal distance away. You can use (-\Xeast,-\Ysouth) to center it vertically as well.

\documentclass[a4paper, 11pt]{article}
\usepackage{tikz}
\begin{document}

This is a line of text which has been written with no purpose
other than to help illustrate how the figure below appears on a page.

\begin{figure}[ht]
\centering
\begin{tikzpicture}[rect/.style={rectangle, draw=black, thin}]

\node[rect] (box1) at (0,0) {Hello there};
% Location 1
\node[rect] (box2) at (5,-1) {Hi};
% Location 2
\path (current bounding box.south east);
\pgfgetlastxy{\Xeast}{\Ysouth}
\path (-\Xeast,0);% expand other side
%\draw[red] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}
\end{figure}

This is a line of text which has been written with no purpose
other than to help illustrate how the figure above appears on a page.
\end{document}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.