7

I have a page which already has a very precise layout with many boxes: text, graphics (PNG), etc.

Without changing the current layout, and without changing anything that could impact the next pages, I'd like to add a textbox with a precise position, with something like:

\positiontextbox{3cm}{10cm}{Hello}    %  3 cm from left border of paper
                                      % 10 cm from top border of paper

This would be the LaTeX equivalent of HTML/CSS's #mytextbox { position: fixed; left: 20px; top: 100px; }.

How to do this in LaTeX ?

Basj
  • 764

1 Answers1

7

With the TikZ node current page.north west, it is possible to place some content precisely on a specified position relative to the upper left corner of the page.

\documentclass{article}
\usepackage[a4paper,lmargin=3cm]{geometry}

\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{showframe}

\newcommand{\positiontextbox}[4][]{%
  \begin{tikzpicture}[remember picture,overlay]
%    \draw[step=0.5,gray!80!white] (current page.north west) grid (current page.south east); % For controlling
    \node[inner sep=10pt,right, fill=yellow,draw,line width=1pt,#1] at ($(current page.north west) + (#2,-#3)$) {#4};
  \end{tikzpicture}%
}

\usepackage{blindtext}

\begin{document}
\blindtext[2]

\positiontextbox{3cm}{10cm}{Hello}

\positiontextbox[fill=green]{14cm}{5cm}{Hello World}


\end{document}

enter image description here

The version with the requested circle (The line wrapping is from a very useful hint from Torbjørn T.)

\documentclass{article}
\usepackage[a4paper,lmargin=3cm]{geometry}

\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{showframe}

\newcommand{\positiontextbox}[4][]{%
  \begin{tikzpicture}[remember picture,overlay]
%    \draw[step=0.5,gray!80!white] (current page.north west) grid (current page.south east); % For controlling
    \node[align=left,circle,inner sep=5pt,right, fill=white,draw,#1] at ($(current page.north west) + (#2,-#3)$) {#4};
  \end{tikzpicture}%
}

\usepackage{blindtext}

\begin{document}
\blindtext[2]

\positiontextbox{3cm}{10cm}{Hello}

\positiontextbox[fill=green]{14cm}{5cm}{Hello\\ World}
\end{document}

enter image description here

  • 1
    Thanks @Christian Hupfer! I'm currently playing with it. Little additions: how to remove the border (I did it by removing draw is that the good way to do it?), have a solid white circle background instead? – Basj Apr 10 '17 at 08:49
  • @Basj: Yes, that's the way to do so. I just used the border to make the box more outstanding. With solid white circle background you mean that the box is a white circle actually? –  Apr 10 '17 at 08:52
  • I mean like this @ChristianHupfer: http://imgur.com/a/3qacx – Basj Apr 10 '17 at 08:55
  • @Basj: A white circle in the center of the rectangle box and the text in the middle of the circle? –  Apr 10 '17 at 08:58
  • In my screenshot, the blue background is external to the textbox, it's the background of the page. So the only thing that needs to be drawn is the plain solid white circle + the text inside. – Basj Apr 10 '17 at 09:00
  • 1
    @Basj: Try \node[circle,inner sep=5pt,right, fill=white,#1] at ($(current page.north west) + (#2,-#3)$) {#4}; instead of the other posted \node command –  Apr 10 '17 at 09:04
  • 1
    @Basj: You're welcome. Happy TeXing –  Apr 10 '17 at 09:15