[Tex/LaTex] Insert text into a fixed size box

boxesenvironments

Is there a way to create a box which has fixed width and height? I tried \parbox and some similar things, but sometimes the text overflows.

If the text inserted would overflow: it should be resized to fit or it should be clipped (with a warning). Is any of these possible?

We can assume, that the inserted text is one continous paragraph.

Related: Package for game/playing cards (not only poker)

Best Answer

You can use the fitting library of the tcolorbox package. It provides a macro called \tcboxfit which fits the font size of the content to the dimensions of the box. Alternatively, the option fit for tcolorbox does the same trick.

Note that the used font has to be resizable to all dimensions (vector font) to achieve the correct resizing.

The first example shows a framed box (can be adapted in many ways), the second one a box without a frame (the red border is just for displaying the size), the third example displays a box with title and fixed height.

\documentclass[a4paper]{article}
\usepackage[many]{tcolorbox}
\usepackage{lmodern}% or any other vector / postscript font
\usepackage{lipsum}
\begin{document}

\tcboxfit[width=6cm,height=6cm,nobeforeafter,
  before=\noindent]{\lipsum[1]}
%
%
\begin{tcolorbox}[fit,width=6cm,height=6cm,blank,
  borderline={0.4pt}{0pt}{red!20!white},
  watermark text={6cm $\times$ 6cm},nobeforeafter]
\lipsum[1]
\end{tcolorbox}

\bigskip

\tcboxfit[height=6cm,title={This box has a height of 6cm},
  colback=blue!5!white,colframe=blue!30!black,
  before=\noindent]{\lipsum[1]}

\end{document}

enter image description here