18

I have read this thread, but I don't think it addresses the same problem.

I am looking for a way to have a command in LaTeX with an interface as follows:

\placetextbox{x_in_page_percentage, y_in_page_percentage}{This is my text}

The command would place the text This is my text centered within a box that may overlap with anything else on the page, and that would be placed at the coordinates specified by the variables x_in_page_percentage and y_in_page_percentage. These variables control the location of the x in the diagram below. E.g.

-------------x---------------
      This is my text
-----------------------------

This way, something like \placetextbox{0.5, 0.5}{This is my text} would place the text roughly on the center of the page, regardless of anything else that may be on that location.

Any thoughts how to do this?

  • The title of your question is confusing. Normally, when discussing LaTeX, a "floating" object does not overlap any other visible objects in the document. What makes it a "floating" is that its location in the output document is not exactly the location in the source code. e.g. you embed an image file. In your source code, you have some text before the image embed and some text after the image embed If latex put the picture exactly where your embed code was, then half of the picture might be on one page, and half on another page. As such, LaTeXwill "float" the picture somewhere else. – IdleCustard Jan 11 '19 at 03:11

2 Answers2

30

The eso-pic package allows you to add pictures (and text) to pages as an overlay/in the foreground [as an underlay/in the background]. The specific command used to add content to the current page is \AddToShipoutPictureFG* [\AddToShipoutPictureBG*]. The starred * versions of these commands limit the output to the current page, while the unstarred versions ship its contents out to every page.

The following minimal example illustrates the technique used to answer your question in the form of a macro \placetextbox{<horizontal pos>}{<vertical pos>}{<stuff>}:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{lipsum}% For 'Lorem Ipsum' dummy text
\usepackage[pscoord]{eso-pic}% The zero point of the coordinate systemis the lower left corner of the page (the default).

\newcommand{\placetextbox}[3]{% \placetextbox{<horizontal pos>}{<vertical pos>}{<stuff>}
  \setbox0=\hbox{#3}% Put <stuff> in a box
  \AddToShipoutPictureFG*{% Add <stuff> to current page foreground
    \put(\LenToUnit{#1\paperwidth},\LenToUnit{#2\paperheight}){\vtop{{\null}\makebox[0pt][c]{#3}}}%
  }%
}%

\begin{document}
\lipsum[1-5]%
\placetextbox{0.5}{0.5}{\fbox{\Huge\textsf{This is my text.}}}%
\placetextbox{0.5}{1}{\Huge\texttt{Here is another piece of text.}}%
\placetextbox{0.1}{0.1}{\Large $\mathcal{A}_1$}%
\end{document}

Placement of text at absolute position on page

Other packages are also capable of doing this, including background and tikz.

Werner
  • 603,163
  • Thanks @Werner. I get the following error with eso_pic: "Option clash for package eso-pic". Before that, I get a warning that says "You have requested the package ;eso-pic', but the package provides 'everyshi'" Any thoughts why I get this? – Amelio Vazquez-Reina Aug 02 '11 at 19:42
  • Are you compiling the minimal example provided above? – Werner Aug 02 '11 at 19:51
  • I just made it compile with the minimal example above. Thanks! Any clues why I may be getting the error "Option clash for the package eso-pic" with a more complex document? – Amelio Vazquez-Reina Aug 02 '11 at 20:03
  • Nevermind. I just made it work. All I had to do is to include it in the document before other packages. Thanks a lot. It's a useful command to add copyright notices in papers, etc. – Amelio Vazquez-Reina Aug 02 '11 at 20:12
  • It doesn't work for me. It moves everything on the next page... – Karel Bílek Jun 05 '12 at 23:31
  • 1
    @KarelBílek: Perhaps you could ask fresh one using the "Ask Question" link above. Follow-up questions like this are more than welcome! Please also include a link to this question to provide the background. Merely stating "it doesn't work for me" doesn't provide any detail on the reasons, or perhaps other packages that you're loading. – Werner Jun 05 '12 at 23:36
  • @Werner How can I create a line break within the text box? The box with your code does not let me create line breaks. – Cookie Dec 07 '17 at 07:16
  • 3
    @Cookie: You can use \AddToShipoutPictureFG*{\put(\LenToUnit{#1\paperwidth},\LenToUnit{#2\paperheight}){\makebox[0pt][c]{\begin{tabular}{l}#3\end{tabular}}}}. I just placed a tabular around the #3 argument. This allows you to use \\ to break a line wherever you want. The alignment will be left, but you can change that to whatever you want. – Werner Dec 07 '17 at 16:13
  • Is there a way to do this with a specified text width? Because if I write a long sentence, it is all displayed in a single line (which is longer than the text width). – Fabian Ying Sep 20 '18 at 15:13
  • 2
    @FabianYing: Instead of \makebox[0pt][c]{#3}, you can use \parbox{<width>}{#3} where <width> is something you specify... like \textwidth. If you want it centred, use \parbox{\textwidth}{\centering #3}. – Werner Sep 23 '18 at 02:38
2

I made some improvements to the \placetextbox macro, written by @Werner, including the alignment improvement from comment 3. The issue with relative positioning to the current page, is that on right alined text, the content may go outside of the page, since the left (or center) of the box is used.

The text alignment must now be specified using the #3 argument. Depending on l or r, the vertical pos is now an offset from the left or right respectively from the page edge and horizontal pos is the offset from the top. A unit should be specified, in this case cm.

\newcommand{\placetextbox}[4]{% \placetextbox{<offset top>}{<offset left/right>}{<align>}{<stuff>}
  \setbox0=\hbox{#4}% Put <stuff> in a box
  \AddToShipoutPictureFG*{% Add <stuff> to current page foreground
    \if#3r
    \put(\LenToUnit{\paperwidth-#1},\LenToUnit{\paperheight-#2}){\vtop{{\null}\makebox[0pt][r]{\begin{tabular}{r}#4\end{tabular}}}}%
    \else
    \put(\LenToUnit{#1},\LenToUnit{\paperheight-#2}){\vtop{{\null}\makebox[0pt][l]{\begin{tabular}{l}#4\end{tabular}}}}%
    \fi
  }%
}%

Example usage:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{lipsum}% For 'Lorem Ipsum' dummy text
\usepackage[pscoord]{eso-pic}% The zero point of the coordinate systemis the lower left corner of the page (the default).

% include macro from codeblock above

\begin{document} \lipsum[1-5]% \placetextbox{0.1cm}{2cm}{r}{This text should nicely align the the right side \ at the top of the page} \placetextbox{0.1cm}{10cm}{l}{\Huge\texttt{Large Text on the left}}% \end{document}

Output: textbox-example