# Centering text in \adjustbox minipage with \hfill

I need to center text in a adjustbox package with a minipage keyword (see code below). I found I can do this by using two \dotfill or \hrulefill commands on either side of the text. Surprisingly, however, \hfill does not work.

I provide a minimal working example:

\documentclass[12pt]{article}
\begin{document}
{\bf These work as expected:}\\

\vspace{2ex}
\verb!{\dotfill{Foo}\dotfill}!\\

\vspace{2ex}
\verb!{\hrulefill{Foo}\hrulefill}!\\

\vspace{2ex}
\verb!{\hfill{Foo}\hrulefill}!\\

\vspace{5ex}
{\bf These don't:}\\

\vspace{2ex}
\verb!{\hfill{Foo}\hfill}!\\

\vspace{2ex}
\verb!{\hrulefill{Foo}\hfill}!\\
\end{document}


My two questions are:

1. How can I center my text in a \adjustbox minipage?
2. Why such a behavior of \hfill?
• There is no object for \hfill to push right. Use \hfill Foo \hfill\mbox{} then you get the desired result. You can also use \hfil Foo – user11232 Sep 16 '15 at 15:37
• All those \\  at ends of paragraph are generating Underfull \hbox (badness 10000) in paragraph at lines 19--20 that is infinitely bad lines. Also why \bf ? – David Carlisle Sep 16 '15 at 15:43
• \bf is for clarity. I didn't know that it was bad usage of \. What should I do instead? – MarcinKonowalczyk Sep 16 '15 at 15:46
• @HarishKumar, This is exactly what I've needed. If you make it an answer I'll happily accept it. – MarcinKonowalczyk Sep 16 '15 at 15:47
• Never end a paragraph with \\  just delete it you don't need to do anything instead. – David Carlisle Sep 16 '15 at 15:48

You should place a "marker" at the end of the line where \hfill is used to avoid the glue being gobbled; something like \mbox{} works:

\documentclass{article}
\begin{document}

These work as expected:

\medskip
\verb!{\dotfill{Foo}\dotfill}!\\

\medskip
\verb!{\hrulefill{Foo}\hrulefill}!\\

\medskip
\verb!{\hfill{Foo}\hrulefill}!\\

\bigskip\bigskip
These do as well:

\medskip
\verb!{\hfill{Foo}\hfill\mbox{}}!\\

\medskip
\verb!{\hrulefill{Foo}\hfill\mbox{}}!\\

\end{document}

\documentclass[12pt]{article}

• The fact that \par is needed looks like a feature that might be worth reporting to the adjustbox author. – David Carlisle Sep 16 '15 at 15:53