[Tex/LaTex] Center text around certain position using overpic

floatshorizontal alignmentoverpicparbox

I am using the overpic package to place text inside of a figure.
For using line breaks inside a put command I use parbox (see example code).

\documentclass{article}
\usepackage{graphicx}
\usepackage[percent]{overpic}

\begin{document}

\begin{figure}[h!]
\centering
\begin{overpic}[width=0.3\textwidth]{example-image}
\put(56,47){\parbox{10cm}{some\newline too long text}}
\end{overpic}
\end{figure}

\end{document}

Using this code, the text is aligned left at the specified position of the put command.
How can I center the text around the specified position in the put command so that in the example code "some" should appear somewhere above "long"? Note that using centering centers the text around the middle of the figure which is not what I want.

Best Answer

Your questions seems to have 2 components to it:

  1. Centering the various lines of text relative to each other, and

  2. Centering the block of text relative to the \put position.

For #1, I choose to use a stack, which also helps with #2, where I place the stack in a zero-width \makebox.

The MWE, where I also place a red dot at the same location, so that we can see that these \puts are centered about the \put point.

\documentclass{article}
\usepackage{graphicx,xcolor}
\usepackage[usestackEOL]{stackengine}
\usepackage[percent]{overpic}

\begin{document}

\begin{figure}[h!]
\centering
\begin{overpic}[width=0.3\textwidth]{example-image}
\put(56,47){\makebox[0pt]{\Centerstack{some\\ too long text}}}
\put(56,47){\makebox[0pt]{\Huge\textcolor{red}{.}}}
\end{overpic}
\end{figure}

\end{document}

enter image description here

I might also suggest the alternative of using a \stackinset instead of an overpic. The horizontal alignment of l, c, r (argument 1) and the vertical alignment of t, c, b (argument 3) non only tell where on the background image to use as the alignment reference, but also where on the inset to use as the reference. Thus the use of c for both of these will guarantee that the center of the inset is placed relative to the center of the image, at the offsets provided.

\documentclass{article}
\usepackage{graphicx,xcolor}
\usepackage[usestackEOL]{stackengine}
\usepackage[percent]{overpic}
\begin{document}
\begin{figure}[h!]
\centering
\stackinset{c}{6pt}{c}{12pt}{\Centerstack{some\\too long text}}
{\includegraphics[width=0.3\textwidth]{example-image}}
\end{figure}
\end{document}

enter image description here

Related Question