# Center text around certain position using overpic

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.

• \centering some\par instead of the \newline. – Johannes_B Oct 31 '17 at 8:52

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}


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}


Simple and complex. Simple is with tabular, complex is with varwidth.

The bullet is just to show the reference point.

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

\begin{document}

\begin{overpic}[width=0.3\textwidth]{example-image}
\put(56,47){\begin{tabular}{@{}c@{}}some\\too long text\end{tabular}}
\put(56,47){\textbullet}
\end{overpic}

\bigskip

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

\end{document}


In order to make the given coordinates the center of the added text, enclose it in \makebox(0,0){...}:

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

\begin{document}

\begin{overpic}[width=0.3\textwidth]{example-image}
\put(56,47){\makebox(0,0){\begin{tabular}{@{}c@{}}some\\too long text\end{tabular}}}
\put(56,47){\makebox(0,0){\textbullet}}
\end{overpic}

\bigskip

\begin{overpic}[width=0.3\textwidth]{example-image}
\put(56,47){\makebox(0,0){\begin{varwidth}{10cm}\centering some\\too long text\end{varwidth}}}
\put(56,47){\makebox(0,0){\textbullet}}
\end{overpic}

\end{document}