Scale an equation to fit exact page width

Latex

Latex Problem Overview


I have an equation that is only a tiny bit too wide for one line. I'd really like to avoid having the equation number on the next line. How do you achieve this? Currently I'm using \small, but this is overkill.

Please note. I have tried scalebox and fittowidth but get errors about missing \endgroup. I have also used \! to its full extent. I am hoping for a solution that will allow me to scale the proper one-line equation to the width of the page.

Here is an example (not my actual equation): Long equation

Latex Solutions


Solution 1 - Latex

\begin{equation}
\resizebox{.9\hsize}{!}{$A+B+C+D+E+F+G+H+I+J+K+L+M+N+O+P+Q+R+S+T+U+V+W+X+Y+Z$}
\end{equation}

or

\begin{equation}
\resizebox{.8\hsize}{!}{$A+B+C+D+E+F+G+H+I+J+K+L+M+N+O+P+Q+R+S+T+U+V+W+X+Y+Z$}
\end{equation}

Solution 2 - Latex

I just had the situation that I wanted this only for lines exceeding \linewidth, that is: Squeezing long lines slightly. Since it took me hours to figure this out, I would like to add it here.

> I want to emphasize that scaling fonts in LaTeX is a deadly sin! In nearly every situation, there is a better way (e.g. multline of the mathtools package). So use it conscious.

In this particular case, I had no influence on the code base apart the preamble and some lines slightly overshooting the page border when I compiled it as an eBook-scaled pdf.

\usepackage{environ}         % provides \BODY
\usepackage{etoolbox}        % provides \ifdimcomp
\usepackage{graphicx}        % provides \resizebox

\newlength{\myl}
\let\origequation=\equation
\let\origendequation=\endequation

\RenewEnviron{equation}{
  \settowidth{\myl}{$\BODY$}                       % calculate width and save as \myl
  \origequation
  \ifdimcomp{\the\linewidth}{>}{\the\myl}
  {\ensuremath{\BODY}}                             % True
  {\resizebox{\linewidth}{!}{\ensuremath{\BODY}}}  % False
  \origendequation
}

Before before After after

Solution 3 - Latex

The graphicx package provides the command \resizebox{width}{height}{object}:

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\hrule
%%%
\makeatletter%
\setlength{\@tempdima}{\the\columnwidth}% the, well columnwidth
\settowidth{\@tempdimb}{(\ref{Equ:TooLong})}% the width of the "(1)"
\addtolength{\@tempdima}{-\the\@tempdimb}% which cannot be used for the math
\addtolength{\@tempdima}{-1em}%
% There is probably some variable giving the required minimal distance
% between math and label, but because I do not know it I used 1em instead.
\addtolength{\@tempdima}{-1pt}% distance must be greater than "1em"
\xdef\Equ@width{\the\@tempdima}% space remaining for math
\begin{equation}%
\resizebox{\Equ@width}{!}{$\displaystyle{% to get everything inside "big"
 A+B+C+D+E+F+G+H+I+J+K+L+M+N+O+P+Q+R+S+T+U+V+W+X+Y+Z}$}%
\label{Equ:TooLong}%
\end{equation}%
\makeatother%
%%%
\hrule
\end{document}

Solution 4 - Latex

Just an addition to the answer by Stephen: I use something like

\resizebox{\minof{\width}{\myeqwidth}}{!}{%
  a = b
}

together with the calc package (which provides \minof). It seems \width refers to the original width of the box (https://latexref.xyz/_005cresizebox.html), and \myeqwidth is a latex length e.g. defined as \textwidth - 2cm. Thus, if the equation is narrow enough, it is not scaled. If it is too wide, it is scaled to \myeqwidth.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGeoffView Question on Stackoverflow
Solution 1 - LatexAlexey MalistovView Answer on Stackoverflow
Solution 2 - LatexSuuuehgiView Answer on Stackoverflow
Solution 3 - LatexStephenView Answer on Stackoverflow
Solution 4 - LatexRalfView Answer on Stackoverflow