Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to write a command or similar that allows me to rescale a given paragraph to fit a given frame, keeping the aspect ratio. The problem is that the paragraph is usually manually set and not automatically wrapped (and thus I do not know its width).

To give an example, using the adjustbox package, I can combine minsizebox and maxsizebox to define almostwhatiwant in the following way (the boxes generated with displayconstraint are just for illustration):

\documentclass{article}
\usepackage{adjustbox}
\newcommand{\displayconstraint}[1]{\fbox{\begin{minipage}[l][3cm]{5cm} #1 \end{minipage}}}
\newcommand{\almostwhatiwant}[1]{\maxsizebox{5cm}{3cm}{\minsizebox{5cm}{3cm}{#1}}}
\begin{document}\noindent
    \displayconstraint{\almostwhatiwant{test}} % example 1
    \displayconstraint{\almostwhatiwant{t}}\\ % example 2
    \displayconstraint{\almostwhatiwant{test\\test test}} % example 3
    \displayconstraint{\almostwhatiwant{\parbox{\linewidth}{test\\test test}}} % example 4
\end{document}

renders the following:

enter image description here

The first two examples (top row) are rendered as desired, but as soon as I want to make linebreaks, I encounter the problem that I have to use a paragraph-like environment (otherwise, the third example happens), for which I have to specify a linewidth. This allows me to format the text in the way I want, but its width as used by the rescaling boxes is now the specified linewidth and thus the result is dissatisfactory (example 4).

I am aware of many other approaches to this problem, but they all eventually fail due to similar reasons or require me to do something manually (like detecting the longest line of the paragraph).

I could fix this problem, if I could somehow detect the actual width of a paragraph or crop a paragraph to its actual content.

share|improve this question
    
If you are manually linebrealing use \begin{tabular}{@{}l@{}}zzz\\zzz\end{tabular} to make a box as wide as the longest line –  David Carlisle Aug 11 at 9:11
    
Somewhat related (if you need autowrap): tex.stackexchange.com/questions/123614/… –  Steven B. Segletes Aug 11 at 10:54
    
@StevenB.Segletes: But that solution only works, if the text is long enough to be autwrapped to a certain height and if I am not mistaken, it cannot be easily tweaked to do what I want, right? –  Wrzlprmft Aug 11 at 11:05
    
@Wrzlprmft That is a good point. I had not realized the limitation of needing a line long enough to wrap to begin with. I'll think some more. –  Steven B. Segletes Aug 11 at 11:08

1 Answer 1

up vote 4 down vote accepted

You can use a tabular or varwidth

enter image description here

\documentclass{article}
\usepackage{adjustbox,varwidth}
\newcommand{\displayconstraint}[1]{\fbox{\begin{minipage}[l][3cm]{5cm} #1 \end{minipage}}}
\newcommand{\almostwhatiwant}[1]{\maxsizebox{5cm}{3cm}{\minsizebox{5cm}{3cm}{#1}}}
\begin{document}

\raggedright
    \displayconstraint{\almostwhatiwant{test}} % example 1
    \displayconstraint{\almostwhatiwant{t}}\\ % example 2
    \displayconstraint{\almostwhatiwant{test\\test test}} % example 3
    \displayconstraint{\almostwhatiwant{\begin{tabular}{@{}l@{}}test\\test test\end{tabular}}} % example 4


\displayconstraint{\almostwhatiwant{\begin{varwidth}{\linewidth}
test test
\end{varwidth}}}
\displayconstraint{\almostwhatiwant{\begin{varwidth}{\linewidth}
test test test test
\end{varwidth}}}
\displayconstraint{\almostwhatiwant{\begin{varwidth}{\linewidth}
test test test test test test
\end{varwidth}}}
\displayconstraint{\almostwhatiwant{\begin{varwidth}{\linewidth}
test test test test test test test test
\end{varwidth}}}
\displayconstraint{\almostwhatiwant{\begin{varwidth}{\linewidth}
test test test test test test test test test test
\end{varwidth}}}

\end{document}
share|improve this answer
    
+1. Using tabulars is definetely a huge step forward, in particular since I can move \begin{tabular} and end{tabular} into the definition of \almostwhatiwant. Unfortunately, this solution cannot cope with the text exceeding some predefined width and being autowrapped in some cases – so I would need two variants of \almostwhatiwant, one for short-lined and one for long-lined, autowrapped texts. –  Wrzlprmft Aug 11 at 10:53
    
@Wrzlprmft you could use the varwidth package to give you an environment with a max witth I'll add an example –  David Carlisle Aug 11 at 11:07
    
@Wrzlprmft updated with varwidth version –  David Carlisle Aug 11 at 11:16
    
Wonderful. Indeed, there is no need for tabular anymore. –  Wrzlprmft Aug 11 at 11:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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