3

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}
\usepackage{adjustbox}
\begin{document}
    {\bf These work as expected:}\\

    \vspace{2ex}
    \verb!{\dotfill{Foo}\dotfill}!\\
    \adjustbox{fbox,minipage=\textwidth}{\dotfill{Foo}\dotfill}

    \vspace{2ex}
    \verb!{\hrulefill{Foo}\hrulefill}!\\
    \adjustbox{fbox,minipage=\textwidth}{\hrulefill{Foo}\hrulefill}

    \vspace{2ex}
    \verb!{\hfill{Foo}\hrulefill}!\\
    \adjustbox{fbox,minipage=\textwidth}{\hfill{Foo}\hrulefill}

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

    \vspace{2ex}
    \verb!{\hfill{Foo}\hfill}!\\
    \adjustbox{fbox,minipage=\textwidth}{\hfill{Foo}\hfill}

    \vspace{2ex}
    \verb!{\hrulefill{Foo}\hfill}!\\
    \adjustbox{fbox,minipage=\textwidth}{\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?
  • 2
    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
  • 1
    Never end a paragraph with \\ just delete it you don't need to do anything instead. – David Carlisle Sep 16 '15 at 15:48
1

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:

enter image description here

\documentclass{article}
\usepackage{adjustbox}
\begin{document}

These work as expected:

\medskip
\verb!{\dotfill{Foo}\dotfill}!\\
\adjustbox{fbox,minipage=\textwidth}{\dotfill{Foo}\dotfill}

\medskip
\verb!{\hrulefill{Foo}\hrulefill}!\\
\adjustbox{fbox,minipage=\textwidth}{\hrulefill{Foo}\hrulefill}

\medskip
\verb!{\hfill{Foo}\hrulefill}!\\
\adjustbox{fbox,minipage=\textwidth}{\hfill{Foo}\hrulefill}

\bigskip\bigskip
These do as well:

\medskip
\verb!{\hfill{Foo}\hfill\mbox{}}!\\
\adjustbox{fbox,minipage=\textwidth}{\hfill{Foo}\hfill\mbox{}}

\medskip
\verb!{\hrulefill{Foo}\hfill\mbox{}}!\\
\adjustbox{fbox,minipage=\textwidth}{\hrulefill{Foo}\hfill\mbox{}}

\end{document}
| improve this answer | |
3
\documentclass[12pt]{article}
\usepackage{adjustbox}
\begin{document}
    \textbf{These work as expected:}%\\

    \vspace{2ex}

    \adjustbox{fbox,minipage=\textwidth}{\centering Foo\par}


\end{document}
| improve this answer | |
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.