1

Consider the following LaTeX document:

\documentclass{article}
\usepackage{lipsum,tikz}
\begin{document}
   \lipsum[1-1]

   \tikz \draw (0,0) rectangle (1,1);
\end{document}

When viewed in a PDF viewer, the corresponding PDF file looks like this (I have truncated the lower part of the page from the screenshot):

Horizontal positioning of a TikZ picture

How can I adjust the horizontal positioning of the TikZ picture more precisely with respect to the page's/text's edges/center? For example, suppose I wish to the picture's center to be 1cm to the right of the center of the text? Or 1cm to the left of the text's right edge? Or 1cm to the left of the page's right edge? Or half way between the text's right edge and the page's right edge? And what if, instead of the picture's center I'd like the reference point to be some other anchor?

5
  • 1
    You can plase a \tikzmark where you wish to place an anchor which you can then use in a tikzpicture that has remember picture, overlay as options. See ctan.org/pkg/tikzmark. You can also use the page anchors, such as current page.west. I think there should be some examples on this site already. Nov 11, 2022 at 11:26
  • It is positioned like a x There is nothing specific to tikz in the question. What your image shows is two paragraphs, each starting with an indent of \parskip, the second paragraph happens to start with a square rather than a letter. Nov 11, 2022 at 11:31
  • @DavidCarlisle So what should I Google to find a solution to my problem? "How to position x horizontally in a LaTeX document"?
    – Evan Aad
    Nov 11, 2022 at 11:33
  • you could look at horizontal-alignment tag here, or look at center or flushleft enviroments or \hspace* or .... Nov 11, 2022 at 11:37
  • 1
    Or google "How to position a box horizontally in a LaTeX documen" and find for example... tex.stackexchange.com/questions/316475/… tex.stackexchange.com/questions/361454/… tex.stackexchange.com/questions/29692/… ... after realizing that a tikzpicture environment is just a (possibly big) box.
    – Rmano
    Nov 11, 2022 at 11:39

2 Answers 2

7

The basic information that seems to be missing here is that a tikzpicture is really a box, like for example a character. Boxes are the fundamental thing in TeX (and LaTeX).

So look at the following example, that I hope is auto-explicative, to have a compendium of all the thing you can find in the Q&A I linked in my comment:

\documentclass{article}
\usepackage{lipsum,tikz}
\usetikzlibrary{tikzmark}
\begin{document}
   \lipsum[1-1]

   The \texttt{tikzpicture} is really a character (or a \emph{box}) for \TeX.
   Some text \tikz \draw (0,0) rectangle (1,1);
   more text where I show that you can
   change where the baseline of the box is:
   \tikz[baseline={(1,1)}] \draw (0,0) rectangle (1,1);
   And more text here
   \tikz[baseline={(0,0.5)}] \draw (0,0) rectangle (1,1);

   So if you have another paragraph here you can put it in the
   margin as you do with any text.
   So if you have another paragraph here you can put it in the
   margin as you do with any text.\marginpar{%
        \tikz \draw (0,0) rectangle (1,1);
   }
   So if you have another paragraph\tikzmark{a}
   here you can put it in the
   margin as you do with any text.

   This one will be set on the mid of the page, 5cm down the top.
   \tikz[overlay, remember picture]
        \draw[red] ([yshift=-5cm]current page.north) rectangle ++(1,1);

   And this one will be at the right of the mark set on the
   third "paragraph" word in the previous paragraph.
   \tikz[overlay, remember picture] \draw[red] (pic cs:a) rectangle ++(1,1);

    And finally, given that the picture is a box, you can display it:
    \begin{center}
        \tikz \draw (0,0) rectangle (1,1);
    \end{center}
    Or even put into an equation:
    \begin{equation}
        A=\tikz[baseline={(0,0.5)}] \draw (0,0) rectangle (1,1);
    \end{equation}

    Or 5cm from the text margin position:

    \noindent\hspace*{5cm}\tikz[baseline={(0,0.5)}] \draw (0,0) rectangle (1,1);

Or two-thirds away between tex margins:
    
    \noindent X\hfill\hfill\tikz \draw (0,0) rectangle (1,1);\hfill X 

\end{document}

enter image description here

Notice how the in-line \tikz are treated as if they were characters, and they change the line spacing accordingly. The overlay option makes the whole tikzpicture appearing as if it where 0-sized. The remember picture is needed so that the tikzmark library can process the positions across the compilation (you probably need to compile a couple of times to have the result shown here).

Also, the "reference" of the picture is not its center. To the external flow of text, what matters is the baseline; the default baseline is the bottom left corner of the whole picture, no matter the internal coordinates; you can change the baseline with the baseline option.

4
  • Thank you. This is informative, but actually does not answer my questions at all. I'm interested in positioning a TikZ picture in "display mode", i.e. one that is its own paragraph; not inline. And I only want to determine its horizontal positioning, not its vertical one; the vertical position is determined automatically by LaTeX the way one paragraph follows the previous one. And I'd like to be able to do this with respect not only to the page's edges, but also to the text's edges.
    – Evan Aad
    Nov 11, 2022 at 12:40
  • 3
    Sorry, I thought that the information was there (basically: you do the same thing you would do to display any random box, as a letter). I added a couple of paragraphs.
    – Rmano
    Nov 11, 2022 at 12:47
  • Regarding the last line of code, then one with the \hfills. How can I use \hfills without the Xs or any other symbol on the line's ends?
    – Evan Aad
    Nov 12, 2022 at 8:07
  • 1
    @EvanAad --- just remove the Xs. You can try, it will not explode... I put them there just to show the margins. But again, you seem to ignore the basic message: the tikzpicture there acts a any other box. Substitute it with \fbox{I'm a box} and nothing changes!
    – Rmano
    Nov 12, 2022 at 16:06
3

It is not entirely clear, what is your intention, but I anticipate that you looking for something like this:

enter image description here

where vertical dashed red lines indicate horizontal center of text.

Positioning of tikzpicture on page can be absolute (as is drawn vertical red lines in MWE below) or relative by positioning of \tikzpicture by use of \vspace* (if needed) and \hspace*, as is done in the following MWE:

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}

\begin{document}
   \lipsum[1-1]
   
\noindent%
%\vspace*{11mm}% if you like vertical shift of image
\hspace*{\dimexpr0.5\linewidth+1cm}%
     \tikz \draw (0,0) rectangle (1,1);
     
     % show horizontal center of paragraph
    \tikz[remember picture, overlay]\draw[dashed,red] (current page.north) -- (current page.south);
\end{document}

Of course, with accordingly settings of the \hspace* as well of \vspace* you can shift your image from the start of the last line in a paragraph to desired position.

2
  • Thank you. In the last sentence, I think you meant that I could shift the image?
    – Evan Aad
    Nov 11, 2022 at 15:22
  • @EvanAad, oh in last sentence was typing error. I correct it and try to make it more clear.
    – Zarko
    Nov 11, 2022 at 15:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .