# How to scale equation only when it is larger than linewidth?

I know that we can adjust the size of equation using:

\resizebox{1\hsize}{!}{}


But I always have to digit that when I have a big equation.

So, how to make latex percept that the equation is larger than linewidth and rescale it automatically.

I think that a new environment should be created...

• Be careful about changing font sizes. As far as most readers are concerned the larger the font the more important the message. If the usual equations are in the normal font size then equations in a smaller font will be considered as less important. Keep the normal fontsize and split the longer equations over two or more lines. Commented Jun 28, 2016 at 18:46

You can define a simple macro, in which you can make a comparison of lengths, here is an attempt using the calc package and \resizebox command from the graphicx package.

\documentclass{article}
\usepackage{graphicx}
\usepackage{calc}

\newlength{\eqhsize}
\newcommand{\myinlineeq}[1]{%
\setlength{\eqhsize}{\minof{\widthof{\mbox{\ensuremath{#1}}}}{\linewidth}}
\resizebox{\eqhsize}{!}{\ensuremath{#1}}}

\begin{document}
$x = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 + 22 + 23 + 24 + 25$

\resizebox{1\hsize}{!}{$x = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 + 22 + 23 + 24 + 25$}

\myinlineeq{x = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 + 22 + 23 + 24 + 25}

\myinlineeq{1}

\end{document}


You should compare the equation length respect to the line width because the effect of resizebox.

Result:

Same idea, only for displayed math. The environments eqnsize and eqnsize* are similar to equation and equation*. Note that \tag and \notag were implemented.

I originally tried to incorporate as much of amsmath as possible, but eventually got hopelessly lost. Comparing eqnsize and eqnsize* one finds a lot of common code that could be moved to a separate macro. I imagine that is how amsmath got so convoluted.

\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{environ}
\usepackage{showframe}

\makeatletter
\newcommand{\mytag}[1]{\setcounter{equation}{\reset}%
\gdef\df@tag{\tagform@{#1}}%
\gdef\@currentlabel{#1}}%

\newcommand{\mynotag}{\setcounter{equation}{\reset}%
\global\let\df@tag\@empty%
\global\let\@currentlabel\@empty}%

\NewEnviron{eqnsize}{\edef\reset{\arabic{equation}}%
\refstepcounter{equation}%
\def\df@tag{\normalfont\normalcolor\tagform@{\theequation}}%
\let\tag=\mytag
\let\notag=\mynotag
\sbox1{$\displaystyle\BODY$}%
\sbox2{\df@tag}%
\@beginparpenalty\predisplaypenalty
\@endparpenalty\postdisplaypenalty
\@topsep\abovedisplayskip
\trivlist
\item\leavevmode
\@tempdima=\dimexpr \wd1+2\wd2+2em\relax
\ifdim\linewidth<\@tempdima
\iftagsleft@ \resizebox{\linewidth}{!}{\hbox to \@tempdima{\rlap{\usebox2}\hfil\usebox1\hfil}}%
\else \resizebox{\linewidth}{!}{\hbox to \@tempdima{\hfil\usebox1\hfil\llap{\usebox2}}}%
\fi
\else
\iftagsleft@ \hbox to \linewidth{\rlap{\usebox2}\hfil\usebox1\hfil}%
\else \hbox to \linewidth{\hfil\usebox1\hfil\llap{\usebox2}}%
\fi
\fi
\endtrivlist}

\NewEnviron{eqnsize*}[0]{\edef\reset{\arabic{equation}}%
\refstepcounter{equation}% hyperref target for \tag
\mynotag
\let\tag=\mytag
\let\notag=\mynotag
\sbox1{$\displaystyle\BODY$}%
\sbox2{\df@tag}%
\@beginparpenalty\predisplaypenalty
\@endparpenalty\postdisplaypenalty
\@topsep\abovedisplayskip
\trivlist
\item\leavevmode
\@tempdima=\dimexpr \wd1+2\wd2+2em\relax
\ifdim\linewidth<\@tempdima
\iftagsleft@ \resizebox{\linewidth}{!}{\hbox to \@tempdima{\rlap{\usebox2}\hfil\usebox1\hfil}}%
\else \resizebox{\linewidth}{!}{\hbox to \@tempdima{\hfil\usebox1\hfil\llap{\usebox2}}}%
\fi
\else
\iftagsleft@ \hbox to \linewidth{\rlap{\usebox2}\hfil\usebox1\hfil}%
\else \hbox to \linewidth{\hfil\usebox1\hfil\llap{\usebox2}}%
\fi
\fi
\endtrivlist}
\makeatother

\begin{document}
In which case one has
\begin{eqnsize}
x = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14
\end{eqnsize}
which can be expanded as
\begin{eqnsize}
x = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20
\end{eqnsize}
if one so desires.
\end{document}