简体   繁体   中英

Variable font height via tables in LaTeX

I've been looking for a more elegant solution to the following typesetting problem. Consider those banners found in print media where the text is aligned like:

B    I    G        T   E   X   T
small text small text small text
m o r e    m e d i u m   t e x t

The font sizes are adjusted so that the height is scaled down for longer lines of text such that each line has equal width. I've written a small script that runs each line separately, compiles, crops the resulting pdf and then \\includegraphics each in a table. This gives the desired effect but requires an both an outside script and pdfcrop (which only crops to a white bounding box). Since much of LaTeX is self-aware, I imagine it would be possible to have it recognize the width of a box and scale it appropriately so that any text fits exactly into the desired width.

Any thoughts or ideas on how a pure LaTeX implementation might work?

EDIT As a supplement to the suggested solution by AVB (since large code in comments looks awful), I've posted below the code used so that others may find it easily.

\documentclass[]{article}
\usepackage[pdftex]{graphicx}
\begin{document}
\begin{table}[l]
\resizebox{10cm}{!}{BIG Text} \\
\resizebox{10cm}{!}{small text small text small text} \\ 
\resizebox{10cm}{!}{Medium Text Medium Text}
\end{table} \end{document} 

First, read the TeX FAQ entry titled Changing the space between letters . Depending on your circumstances, the packages and recommendation in that FAQ may suffice.

Also, take a look at the \\scalebox and \\resizebox commands in the graphicx package. They do what the names imply.

I'm sure that this could be improved upon, and due to different font implementations at different sizes then it isn't going to be exact, but here's a quick-and-dirty way to do it:

\documentclass[10pt]{article}

\usepackage{pgfmath}
\usepackage{anyfontsize}


\newlength{\mywidth}
\newlength{\testwidth}
\setlength{\mywidth}{4in}
\newcommand{\fixedwidth}[1]{%
\settowidth{\testwidth}{#1}%
\pgfmathsetmacro{\x}{round(\mywidth/\testwidth * 10)}%
\pgfmathsetmacro{\y}{round(\mywidth/\testwidth * 15)}%
\bgroup\fontsize{\x pt}{\y pt}\selectfont#1\egroup}

\begin{document}

\fixedwidth{hello world}

\fixedwidth{greetings earthlings}
\end{document}

In practice, it's a little less than the 4in, but the two lines of text do get scaled to the same amount.

Check out the package textfit . Usage:

\scaletowidth{width}{text}

or

\scaletoheight{height}{text}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM