Which LaTeX packages can I use to center, scale, and wrap text such that the following conditions are met:

  • If the text isn't a wide as the container, scale the up text to fit.
  • If the text is wider than the container, scale the text down to fit.
  • In the case that text is scaled down, wrap the text to prevent ending up with one tiny, unreadable paragraph.

I've tried parbox, pbox, tabular, and adjustbox, none quite meeting all three conditions.

Here's the gist:

\documentclass{article}

\usepackage[percent]{overpic}
\usepackage[outline]{contour}
\usepackage{fontspec}
\newfontfamily\headingText[Color=FFFFFF]{KnowYourProduct}

\newcommand{\myFrame}[1]  {
    \begin{overpic}[width=6cm,height=3cm,grid,tics=10]{Template}
        \put (8,25) {
            {\contourlength{0.025em}\contour{black}{\headingText #1}}
        }
    \end{overpic}
}   

\begin{document}

\myFrame{Title}
\myFrame{This Is A Long Title}
\myFrame{This title is so long that if you're still reading it you may want to reevaluate how you are presently squandering your personal time.}

\end{document}

Sorry, but I had enough problems without having to deal with your fonts, etc.

\documentclass{article}
\usepackage{graphicx}

\newlength{\tempwidth}
\newlength{\temptest}
\newcount{\templines}

\newcommand{\myFrame}[1]% #1 = text
{\settowidth{\tempwidth}{#1}%
\ifdim\tempwidth>6cm \templines=0
  \loop\advance\templines by 1
    \temptest = 2\baselineskip% 6cm (width) / 3cm (height) = 2
    \multiply\temptest by \templines
    \multiply\temptest by \templines
    \ifdim\tempwidth>\temptest\repeat
  \divide\temptest by \templines
  \fbox{\resizebox*{6cm}{3cm}{\parbox{\temptest}{#1}}}%
\else \fbox{\resizebox*{6cm}{3cm}{#1}}%
\fi}

\begin{document}

\myFrame{Title}

\myFrame{This Is A Long Title}

\myFrame{This title is so long that if you're still reading it you may want to reevaluate how you are presently squandering your personal time.}

\end{document}

boxes

  • Not looking for the behavior in the middle box, but I'm working with this on a better solution including all my fonts, etc. I think height is also a constraint in the first case: essentially, there's a magical goldilocks zone here. – twip Sep 12 '14 at 7:04
  • The \parbox solution can be used for smaller titles, but at some point it breaks down. – John Kormylo Sep 12 '14 at 13:04
  • Good point. I really had no idea how deep this combination of wants was going to go. – twip Sep 12 '14 at 16:32

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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