1

I am trying to draw a rectangle with a diagonal which are horizontally centered on the page. I tried

\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
%The rectangle:
\draw (-4,-2) -- (4,-2) -- (4, 2) -- (-4,2) -- cycle; 
%The diagonal:
\draw  (-4,2) -- (4,-2);
\end{tikzpicture}

\end{document}

which gives me the desired figure, but it is too much to the left. When I add

 [shift = {((4 cm, 0 cm))}]

to either the rectangle or the the diagonal , it shifts the appropriate component pretty close to the center of the page. However, if I add this command to both components, as in

\draw [shift = {((4 cm, 0 cm))}] (-4,-2) -- (4,-2) -- (4, 2) -- (-4,2) -- cycle; 
\draw [shift = {((4 cm, 0 cm))}] (-4,2) -- (4,-2);

then it looks like I haven't added it to either; the picture is displayed on the left of the page.

Is there a better way to center the triangle and its diagonal on the left of the page, or is there a way to fix this approach? Thank you very much.

2

TikZ computes the bounding box of the picture. So if you shift everything, this just means the bounding box gets adjusted, and the picture remains what it was before the shift. If you want to center the picture on the page, use \centering. Alternatively, you can work with absolute page coordinates, which requires overlay,remember picture.

\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}
\centering
\begin{tikzpicture}
\draw (-4,-2) rectangle (4, 2)  (-4,2) -- (4,-2); 
\end{tikzpicture}
\end{document}

To only center the tikzpicture you could either use

\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}
\draw (-4,-2) rectangle (4, 2)  (-4,2) -- (4,-2); 
\end{tikzpicture}
\end{center}

Normal text.
\end{document}

or any of the answers to this question.

| improve this answer | |
  • Ah thanks! But I see that "\centering" is outside of the tikz environment. Is this a global change? If so, is there a way to switch it off later? – Ovi Jun 30 '19 at 14:04
  • @Ovi Sure. You could either do \begin{minipage}{\textwidth} \centering \begin{tikzpicture} \draw (-4,-2) rectangle (4, 2) (-4,2) -- (4,-2); \end{tikzpicture} \end{minipage} Normal text. , see the answers to tex.stackexchange.com/q/61112/121799, or use \begin{center}\begin{tikzpicture} \draw (-4,-2) rectangle (4, 2) (-4,2) -- (4,-2); \end{tikzpicture}\end{center}. The second possibility adds some vertical space. – user121799 Jun 30 '19 at 14:08
  • Thank you again! – Ovi Jun 30 '19 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.