GeneralCalculating max point size to fit textwidth

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
petersfreeman
Posts: 19
Joined: Wed Apr 28, 2021 9:40 am

Calculating max point size to fit textwidth

Post by petersfreeman »

I want to be able to calculate the maximum point size I can use for a phrase in a serif font so that it fits within \textwidth. For the calculation, there would be two inputs:

Phrase
\textwidth

The output would be the point size of the font. In a simple way, I'm thinking it would go something like this procedurally:

1. Measure Actual Phrase Width: Use LaTeX’s \settowidth command to measure the width of the phrase at a known font size.
2. Scale Proportionally: Once I have the width at a known size, calculate the scaling factor needed to fit within \textwidth.

I have the first part working but I don't know how to calculate the scaling factor:

Code: Select all

Code, edit and compile here:
\documentclass{article}
\usepackage{calc} % For length calculations
\newlength{\phraselength}
\newcommand{\measurephrase}[1]{%
\settowidth{\phraselength}{#1}% Measure the width of the phrase
The width of the phrase ``#1'' is \the\phraselength.%
}
\begin{document}
% Example usage
\measurephrase{Sample Phrase}
\end{document}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cpierquet
Posts: 9
Joined: Thu May 23, 2024 7:40 pm

Re: Calculating max point size to fit textwidth

Post by cpierquet »

I don't know if's the way to achieve it, but it's seems to work (I guess...), but maybe not the optimal way.

Code: Select all

Code, edit and compile here:
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{calc} % For length calculations
\usepackage{graphicx}
\usepackage{pgf}
\newlength{\phraselength}
\newcommand{\measurephrase}[1]{%
\settowidth{\phraselength}{#1}% Measure the width of the phrase
\noindent The width of the phrase ``#1'' is \the\phraselength.\par
\pgfmathsetmacro{\calcscale}{\linewidth/\phraselength}%
\noindent The scale is \calcscale.\par
\noindent \scalebox{\calcscale}[\calcscale]{#1}
}
\begin{document}
% Example usage
\measurephrase{Sample Phrase}
\measurephrase{Other Sample Phrase}
\end{document}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Post Reply