0

I am looking for the same functionality as simc/auto_size_text, a flutter widget that automatically resizes text to fit perfectly within its bounds.

However, I want to achieve this for physically printed flashcards at various sizes. Particularly A6 paper, but a template that I can alter for different page sizes, A4 to business card, would be best.

\fillpage{}
\resizebox{\textwidth}{!}{
?
?

Can LaTex do this? What command to search for, or should I start making a python script to go directly to pdf?

MS-SPO
  • 11,519

2 Answers2

1

The fitting library from tcolorbox can be used to fit text to boxes. Below is a bare minimum example; see the tcolorbox documentation for how to adjust other properties of the box. Paper size can be set via geometry package.

Note: the library never increases the text size by itself. Instead, you need to specify a base font size that is known to be too big, after which the library will scale it down to fit. (You can compare the effects by removing the \tcbset{fit basedim=35pt} line from the code below.)

\documentclass{article}
\usepackage{fix-cm}  % Allow arbitrary font scaling for Computer Modern
\usepackage[margin=0.25in]{geometry}  % Just so that the examples fit on the same page
\usepackage{tcolorbox}
\tcbuselibrary{fitting}
\usepackage{lipsum} % Just for dummy text

\begin{document}

\tcbset{fit basedim=35pt} % tcb doesn't enlarge text; it starts with this base dimensional and shrinks text to fit % So if you need the font to possibly scale up you need to specify a larger size to boot.

\tcboxfit[height=5.8in,width=4.1in]{\lipsum[1]} % A6 size, portrait

\tcboxfit[width=5.8in,height=4.1in]{\lipsum[2]} % A6 size, landscape

\end{document}

enter image description here

Willie Wong
  • 24,733
  • 8
  • 74
  • 106
  • "Note: the library never increases the text size by itself. Instead, you need to specify a base font size that is known to be too big, after which" Critical info, thank you!! – user95184 Jun 14 '23 at 12:56
1

Another possibility is to use shapepar together with \resizebox.

Caveats:

  • \resizebox has its first two arguments "width" and "height"; but \shapepar's \rectangleshape has them as "height" first and then "width". Be careful when you prepare your document.
  • \shapepar can only, as its name suggests, shape a paragraph. So no displayed math (inline math is okay) or anything that adjusts the vertical spacing.
  • \Shapepar works on entire paragraphs; you need to make sure that the paragraph is terminated (via either a blank line or a \par) if you wrap it in \resizebox.

Code:

\documentclass{article}
\usepackage[margin=0.25in]{geometry}
\usepackage{shapepar}   % Use shapepar to build the correct shape of text block
\usepackage{graphicx}   % Use resizebox to scale it to the right size
\usepackage{lipsum}

\begin{document}

% Note that \Shapepar requires the paragraph to be terminated. % This means we must include either a blank line or a \par. \resizebox{5.8in}{!}{\Shapepar{\rectangleshape{4.1}{5.8}} \lipsum[1]\par}

\resizebox{4.1in}{!}{\Shapepar{\rectangleshape{5.8}{4.1}} \lipsum[2]\par} \end{document}

enter image description here

Willie Wong
  • 24,733
  • 8
  • 74
  • 106