3

In a scenario where TeX documents are generated by a script the challenge is to nicely fit text of variable length into boxes of fixed size.

There's a default font size suited for 80% of the cases but if a box would become overfull, the TeX engine* should reduce the font size of that box until the text fits in the box.

Is this possible?

(Sorry for not providing sample code by I think it's clear from the explanation.)

If this is a scenario where LuaTeX works well, I'm happy to look into it.

  • This is possible. You needn't Lua prefix before TeX. The solution with classical TeX can be found at petr.olsak.net/opmac-tricks-e.html#scaleto – wipet Apr 15 '15 at 11:16
  • This seems to be for a given width only, not a multi-line paragraph with many words. – Sir Vivor Apr 15 '15 at 11:22
  • 1
    You should supply a sample code though, why make anyone considering an answer make their own test file? – David Carlisle Apr 15 '15 at 11:27
  • do you mean limit the vertical size to avoid overfull vboxes? – David Carlisle Apr 15 '15 at 11:32
  • Scale nothing if box not full. Reduce font size if necessary to fit text in a fixed box. – Sir Vivor Apr 15 '15 at 11:34
3

Yet again tcolorbox answer. First with a macro version:

\documentclass{article}
\usepackage[fitting]{tcolorbox}
\usepackage{lipsum}
\usepackage{lmodern}
\newtcboxfit{\mybox}[3][]{colback=red!25!olive,
colframe=red!75!olive,fonttitle=\bfseries,
boxsep=1mm,left=0mm,right=0mm,top=0mm,
bottom=0mm,center upper,valign=center,
nobeforeafter,#1,width=#2,height=#3}

\begin{document}
%% \mybox[<optional>]{<width>}{<height>}{<content>}
\mybox{5cm}{3cm}{\lipsum[1]}
\mybox[colback=green!30]{6cm}{4cm}{\lipsum[1]}


\end{document}

enter image description here

Now the environment form:

\documentclass{article}
\usepackage[fitting,skins]{tcolorbox}
\usepackage{lipsum}
\usepackage{lmodern}
\newtcolorbox{fitbox}[3][]{fit,,width=#2,height=#3,boxsep=1pt,valign=center,opacityupper=0.5,
top=0.4\tcbfitdim,bottom=0.4\tcbfitdim,left=0.75\tcbfitdim,right=0.75\tcbfitdim,
enhanced,watermark text={\tcbfitsteps},colframe=blue!75!black,colback=white,#1}

\begin{document}
%\begin{fitbox}[<optional>]{<width>}{<height>}
  %contents
%\end{fitbox}
\begin{fitbox}{4cm}{2cm}
\lipsum[1]
\end{fitbox}
\begin{fitbox}[colback=green!20]{2cm}{4cm}
\lipsum[2]
\end{fitbox}
\begin{fitbox}{1cm}{2cm}
\lipsum[3]
\end{fitbox}


\end{document}

enter image description here

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.