59

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

Geoff
  • 7,935
  • 3
  • 35
  • 43
  • Are there any unnecessary characters in the equation you could delete? Any way you could rewrite it to be a little shorter? (Just in case nobody comes along with a real solution) – David Z Jun 24 '10 at 01:02
  • Also, why not use amsmath's `split` for equations or a `multiline`? Everything else will be a pain to read anyway. – Benjamin Bannier Jun 24 '10 at 01:09
  • If you can, I think you should define some functions and variables to shorten the main expression down. It can be hard to read and understand such a long expression anyway. – mahju Jun 24 '10 at 05:48
  • My actual equation is quite simple. It is inflated due to function names and subscripts with names (like 'x_\mathrm{min}`). You guys are right in general; my question is specific, if perhaps a little theoretical. – Geoff Jun 24 '10 at 12:41

4 Answers4

91
\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}
Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
  • 1
    +1 So, I was really close with `scalebox`. Why does it have to be inside the equation environment? Also, why am I then forced to use in-line math mode instead of display style? Thanks for this good answer. – Geoff Jun 24 '10 at 14:28
  • 5
    `Why does it have to be inside the equation environment?` It is not necessary. As you wish. For example. `Also, why am I then forced to use in-line math mode instead of display style?` Because `\resizebox` deals with box and you have to form such box. Any math inside horizontal box is text mode math. – Alexey Malistov Jun 25 '10 at 08:06
  • @AlexeyMalistov Can you use this also to reduce the size of only a part of a centered equation? Like `$$aaa=\resizebox{...}{!}bbb$$`. – Karlo Aug 18 '17 at 11:47
  • 7
    Attempted to use this with `p_{x,y}(t)=\varepsilon\cdot\Big[(1-\delta)\cdot p_{x,y}(t-1)+\Delta p_{x,y}(t-1,t)+d_{x,y}(t-1,t)\Big]` which failed. Thus I do not consider this a solution to the general problem. – Koenigsberg Jun 14 '18 at 20:58
  • 2
    This solution does not explain that the `graphicx` package must be loaded in for it to work – Moustache Sep 29 '20 at 00:13
8

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

Suuuehgi
  • 4,547
  • 3
  • 27
  • 32
4

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}
Stephen
  • 141
  • 1
  • 7
1

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.

Ralf
  • 1,203
  • 1
  • 11
  • 20