1

Here is the problem. Some of the equations generated by computer algebra programs are long, and there is no way to edit them by hand to break them.

I like to use align* but scale the long ones so they fit the pdf page.

The following are 3 solutions I know about. I prefer the solution given by resizegather package, because it automatically scale any long equation to fit the page. The problem is that resizegather does not work with align*, only with gather*, and there is no alignment on the &= in this case.

Here are the 3 solutions I know about

\documentclass{article}
\usepackage{amsmath}
\usepackage{adjustbox}
\usepackage{resizegather}

\begin{document}

No scaling done

\begin{align*}
U_1 &= (-1)^{4-1} \int{ \frac{F(x) W_1(x) }{a W(x)} \, dx}\\ 
    &= (-1)^{3} \int{  \frac{ \left(e^{x} \left(\left(2+6 x \right) \cos \left(2 x \right)+3 \sin \left(2 x \right)\right)\right) \left(-8 e^{3 x} \sin \left(2 x \right)^{2} \cos \left(2 x \right) x -8 e^{3 x} \cos \left(2 x \right)^{3} x +4 e^{3 x} \sin \left(2 x \right)^{3}+4 e^{3 x} \sin \left(2 x \right) \cos \left(2 x \right)^{2}\right)}{\left(1\right) \left(64 e^{4 x} \sin \left(2 x \right)^{4}+128 e^{4 x} \sin \left(2 x \right)^{2} \cos \left(2 x \right)^{2}+64 e^{4 x} \cos \left(2 x \right)^{4}\right)} \, dx}\\ 
    &= - \int{  \frac{\left(-48 x^{2}-16 x -12\right) e^{4 x} \cos \left(2 x \right)^{2}+8 e^{4 x} \sin \left(2 x \right) \cos \left(2 x \right)+12 e^{4 x}}{64 e^{4 x}} \, dx}
\end{align*}


Use align, and add adjust box to each line
\begin{align*}
U_1 &= \adjustbox{max width=1.2\linewidth}{$(-1)^{4-1} \int{ \frac{F(x) W_1(x) }{a W(x)} \, dx}$}\\ 
    &= \adjustbox{max width=1.2\linewidth}{$(-1)^{3} \int{  \frac{ \left(e^{x} \left(\left(2+6 x \right) \cos \left(2 x \right)+3 \sin \left(2 x \right)\right)\right) \left(-8 e^{3 x} \sin \left(2 x \right)^{2} \cos \left(2 x \right) x -8 e^{3 x} \cos \left(2 x \right)^{3} x +4 e^{3 x} \sin \left(2 x \right)^{3}+4 e^{3 x} \sin \left(2 x \right) \cos \left(2 x \right)^{2}\right)}{\left(1\right) \left(64 e^{4 x} \sin \left(2 x \right)^{4}+128 e^{4 x} \sin \left(2 x \right)^{2} \cos \left(2 x \right)^{2}+64 e^{4 x} \cos \left(2 x \right)^{4}\right)} \, dx}$}\\ 
    &= \adjustbox{max width=1.2\linewidth}{$- \int{  \frac{\left(-48 x^{2}-16 x -12\right) e^{4 x} \cos \left(2 x \right)^{2}+8 e^{4 x} \sin \left(2 x \right) \cos \left(2 x \right)+12 e^{4 x}}{64 e^{4 x}} \, dx}$}
\end{align*}

gather, automatic scaling, but need to align them also. Is it possible?
\begin{gather*}
U_1 = (-1)^{4-1} \int{ \frac{F(x) W_1(x) }{a W(x)} \, dx}\\ 
    = (-1)^{3} \int{  \frac{ \left(e^{x} \left(\left(2+6 x \right) \cos \left(2 x \right)+3 \sin \left(2 x \right)\right)\right) \left(-8 e^{3 x} \sin \left(2 x \right)^{2} \cos \left(2 x \right) x -8 e^{3 x} \cos \left(2 x \right)^{3} x +4 e^{3 x} \sin \left(2 x \right)^{3}+4 e^{3 x} \sin \left(2 x \right) \cos \left(2 x \right)^{2}\right)}{\left(1\right) \left(64 e^{4 x} \sin \left(2 x \right)^{4}+128 e^{4 x} \sin \left(2 x \right)^{2} \cos \left(2 x \right)^{2}+64 e^{4 x} \cos \left(2 x \right)^{4}\right)} \, dx}\\ 
    = - \int{  \frac{\left(-48 x^{2}-16 x -12\right) e^{4 x} \cos \left(2 x \right)^{2}+8 e^{4 x} \sin \left(2 x \right) \cos \left(2 x \right)+12 e^{4 x}}{64 e^{4 x}} \, dx}
\end{gather*}

Scale the whole align, using example from 
%https://tex.stackexchange.com/questions/397388/adjustbox-around-an-align-environment

\sbox0{\begin{adjustbox}{max width=\textwidth}
\parbox{\linewidth}{
\begin{align*}
U_1 &= (-1)^{4-1} \int{ \frac{F(x) W_1(x) }{a W(x)} \, dx}\\ 
    &= (-1)^{3} \int{  \frac{ \left(e^{x} \left(\left(2+6 x \right) \cos \left(2 x \right)+3 \sin \left(2 x \right)\right)\right) \left(-8 e^{3 x} \sin \left(2 x \right)^{2} \cos \left(2 x \right) x -8 e^{3 x} \cos \left(2 x \right)^{3} x +4 e^{3 x} \sin \left(2 x \right)^{3}+4 e^{3 x} \sin \left(2 x \right) \cos \left(2 x \right)^{2}\right)}{\left(1\right) \left(64 e^{4 x} \sin \left(2 x \right)^{4}+128 e^{4 x} \sin \left(2 x \right)^{2} \cos \left(2 x \right)^{2}+64 e^{4 x} \cos \left(2 x \right)^{4}\right)} \, dx}\\ 
    &= - \int{  \frac{\left(-48 x^{2}-16 x -12\right) e^{4 x} \cos \left(2 x \right)^{2}+8 e^{4 x} \sin \left(2 x \right) \cos \left(2 x \right)+12 e^{4 x}}{64 e^{4 x}} \, dx}\\ 
\end{align*}
}
\end{adjustbox}
}\usebox0

\end{document}

Compiled using lualatex gives

enter image description here

The question is: Is it possible to make resizegather with align? so to get automatic scaling when needed, but keep equations aligned at & ?

2 Answers 2

3

[I completely rewrote the answer after the OP explained, in the comments, that hand-editing individual equations for optimal typographic results was not feasible.]

I think we can agree that both the no-scaling and the global-scaling options yield poor results; see the first and second parts of the following screenshot. (The framelines, inserted by the showframe package, indicate the width of the text block.)

However, modifying your line-by-line scaling approach to make it (a) use a maximal width of 0.9\textwidth rather than 1.2\textwidth and (b) insert \displaystyle directives just might be ok; see the third part of the following screenshot. Of course, the greater the native width of the argument of \adjustbox, the worse the readability of the typeset result.

A separate comment: I understand that the \left and \right sizing directives are inserted by your program. If you can get rid of these sizing directives, that would be great. If not, do load the mleftright package and run \mleftright in the preamble. to "fix" the excess-spacing issue caused by the sizing directives.

enter image description here

\documentclass{article}
\usepackage{amsmath,adjustbox}
\usepackage{showframe,xcolor}
\usepackage{mleftright}\mleftright % optional
%% handy shortcut macro:
\newcommand\newadjustbox[2][0.9]{% % default width: 0.9\textwidth
    \adjustbox{max width=#1\linewidth}{$\displaystyle #2$}}

\begin{document}
\noindent
{\color{red}{No scaling}}

\begin{align*}
U_1 &= (-1)^{4-1} \int{ \frac{F(x) W_1(x) }{a W(x)} \, dx}\\ 
    &= (-1)^{3} \int{  \frac{ \left(e^{x} \left(\left(2+6 x \right) \cos \left(2 x \right)+3 \sin \left(2 x \right)\right)\right) \left(-8 e^{3 x} \sin \left(2 x \right)^{2} \cos \left(2 x \right) x -8 e^{3 x} \cos \left(2 x \right)^{3} x +4 e^{3 x} \sin \left(2 x \right)^{3}+4 e^{3 x} \sin \left(2 x \right) \cos \left(2 x \right)^{2}\right)}{\left(1\right) \left(64 e^{4 x} \sin \left(2 x \right)^{4}+128 e^{4 x} \sin \left(2 x \right)^{2} \cos \left(2 x \right)^{2}+64 e^{4 x} \cos \left(2 x \right)^{4}\right)} \, dx}\\ 
    &= - \int{  \frac{\left(-48 x^{2}-16 x -12\right) e^{4 x} \cos \left(2 x \right)^{2}+8 e^{4 x} \sin \left(2 x \right) \cos \left(2 x \right)+12 e^{4 x}}{64 e^{4 x}} \, dx}
\end{align*}

\bigskip\noindent
{\color{red}Global scaling (to \verb+\textwidth+) }

\noindent % <-- new
\begin{adjustbox}{max width=\textwidth}
\parbox{\linewidth}{%
\begin{align*}
U_1 &= (-1)^{4-1} \int{ \frac{F(x) W_1(x) }{a W(x)} \, dx}\\ 
    &= (-1)^{3} \int{  \frac{ \left(e^{x} \left(\left(2+6 x \right) \cos \left(2 x \right)+3 \sin \left(2 x \right)\right)\right) \left(-8 e^{3 x} \sin \left(2 x \right)^{2} \cos \left(2 x \right) x -8 e^{3 x} \cos \left(2 x \right)^{3} x +4 e^{3 x} \sin \left(2 x \right)^{3}+4 e^{3 x} \sin \left(2 x \right) \cos \left(2 x \right)^{2}\right)}{\left(1\right) \left(64 e^{4 x} \sin \left(2 x \right)^{4}+128 e^{4 x} \sin \left(2 x \right)^{2} \cos \left(2 x \right)^{2}+64 e^{4 x} \cos \left(2 x \right)^{4}\right)} \, dx}\\ 
    &= - \int{  \frac{\left(-48 x^{2}-16 x -12\right) e^{4 x} \cos \left(2 x \right)^{2}+8 e^{4 x} \sin \left(2 x \right) \cos \left(2 x \right)+12 e^{4 x}}{64 e^{4 x}} \, dx}\\ 
\end{align*}
}
\end{adjustbox}


\bigskip\noindent
{\color{red}Add \verb+\adjustbox{max width=0.9\linewidth}{$\displaystyle+ to each line}
\begin{align*}
U_1 &= \newadjustbox{(-1)^{4-1} \int{ \frac{F(x) W_1(x) }{a W(x)} \, dx}}\\ 
    &= \newadjustbox{(-1)^{3} \int{  \frac{ \left(e^{x} \left(\left(2+6 x \right) \cos \left(2 x \right)+3 \sin \left(2 x \right)\right)\right) \left(-8 e^{3 x} \sin \left(2 x \right)^{2} \cos \left(2 x \right) x -8 e^{3 x} \cos \left(2 x \right)^{3} x +4 e^{3 x} \sin \left(2 x \right)^{3}+4 e^{3 x} \sin \left(2 x \right) \cos \left(2 x \right)^{2}\right)}{\left(1\right) \left(64 e^{4 x} \sin \left(2 x \right)^{4}+128 e^{4 x} \sin \left(2 x \right)^{2} \cos \left(2 x \right)^{2}+64 e^{4 x} \cos \left(2 x \right)^{4}\right)} \, dx}}\\ 
    &= \newadjustbox{- \int{  \frac{\left(-48 x^{2}-16 x -12\right) e^{4 x} \cos \left(2 x \right)^{2}+8 e^{4 x} \sin \left(2 x \right) \cos \left(2 x \right)+12 e^{4 x}}{64 e^{4 x}} \, dx}}
\end{align*}

\end{document}
7
  • 1
    Thanks for the answer. But what I meant by these are auto-generated, is that each time the program I have runs, new set of equations are generated (10's of thousands of them). The program then takes the equations and adds the align* code around them. So it is not possible to manually edit these by hand each time the program runs and add something inside them like you did. Everything has to be done by code and automated.. I can only modify the program to change the environment itself around the actual equations generated as shown above. But can't look inside them and edit them.
    – Nasser
    Oct 25, 2020 at 5:06
  • I used to use breqn package long time ago, but that had so many problems. So the next best thing, is to scale some of these. Scaling is OK, since not too many equations are very long, and this is better than having those run off the page and one can't read them. resizegather works well, I just need the equations to be aligned, that is all.
    – Nasser
    Oct 25, 2020 at 5:07
  • @Nasser - Having a long equation run off the side of the page is certainly even worse than having to scale it down. With tens of thousands of equations to consider, hand-editing them is definitely out of the question. That said, I wonder if you're doing your readers any service by showing the intermediate, potentially very long lines of the multi-line equations. Won't they be ok with being shown just the last, simplified line of each equation?
    – Mico
    Oct 25, 2020 at 5:17
  • 1
    The program I am developing is meant to do exactly this. i.e. show all intermediate steps. I wrote this program myself. It solves each problem step-by-step, just like one does by hand. This is the whole point of it. If I only just show the last step, then there is no point for the program, as one can just use CAS itself to see final result. Here is a link to it if you like to see the output. 12000.org/my_notes/solving_ODE/current_version/index.htm I have been working on this for 2 years now. Just some equations can be long, and needed to solve this issue by scaling, when needed.
    – Nasser
    Oct 25, 2020 at 5:20
  • 1
    Mico, thanks! Your adding displaystyle did the trick. Now it looks like gather* but aligned. I also added \usepackage{mleftright} and \mleftright. I have no control over the \left( and \right), since these come when I call Maple Latex conversion to convert the actual equations to Latex. So can't really do anything about these. I can control the Latex that my program itself generates only, not the one that Maple generates. I'll start a new build now to test your method on the whole pdf file (12,000 pages) to check. But on the initial test, it looks good and will use it. Thanks.
    – Nasser
    Oct 25, 2020 at 7:02
3

I avoiding to scale equation. This almost always leads to unreadable result or at least to very poor typography. To me is smaller sin to allow equation that spill out of text borders and only slightly reduce font size in equation:

enter image description here

This can be achieved by use of the nccmath and changepage packages:

\documentclass{article}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{nccmath}
\usepackage[strict]{changepage}

\usepackage{lipsum}

\begin{document}

Let allow, that this extreme wide equation may has a slightly smaller font size and that it can spill out of text borders. This can be achieved with use of \verb+\medmath+ defined in the \verb+nccmath+ package and \verb+adjustwidth+ environments defined in the \verb+changepage+ package:
    \begin{adjustwidth}{-\marginparwidth}{-\marginparwidth}
\[\medmath{
    \begin{aligned}
U_1 &= (-1)^{4-1} \int{ \frac{F(x) W_1(x) }{a W(x)} \, dx}\\
    &= (-1)^{3} \int
    \frac{ \bigl(e^{x} (2+6x) \cos(2x) + 3 \sin(2x)\bigr)
           \bigl(-8 e^{3x}\sin(2x)^{2}\cos(2x)x - 8 e^{3x}\cos(2x)^{3} x +4 e^{3x}
                 \sin(2x)^{3}+4e^{3x} \sin(2x) \cos(2x)^{2}\bigr)}
        {(1) \bigl(64e^{4x}\sin(2x)^{4} + 128e^{4x}\sin(2x)^{2}\cos(2x)^{2} + 64e^{4x}
                \cos(2x)^{4}\bigr)}
                \, dx  \\
U_1 &= - \int
        \frac{(-48x^{2} - 16x -12)e^{4x}\cos(2x)^{2} + 8e^{4x}\sin(2x)\cos(2x) + 12e^{4x}}{64e^{4x}} \, dx
    \end{aligned}
        }
\]
    \end{adjustwidth}
\lipsum[66]
\end{document}

Even nicer result can be obtained by use of the \splitfrac from mathtools package (as propose @Mico in now deleted part of his answer), however, you for some reason don't like to have:

enter image description here

\lipsum[66]
    \begin{adjustwidth}{-\marginparwidth}{-\marginparwidth}
\[\medmath{
    \begin{aligned}
U_1 &= (-1)^{4-1} \int{ \frac{F(x) W_1(x) }{a W(x)} \, dx}\\
    &= (-1)^{3} \int
    \frac{\splitfrac{\bigl(e^{x} (2+6x) \cos(2x) + 3 \sin(2x)\bigr)\cdot}
                    {\bigl(-8 e^{3x}\sin(2x)^{2}\cos(2x)x - 8 e^{3x}\cos(2x)^{3} x +4 e^{3x}
                 \sin(2x)^{3}+4e^{3x} \sin(2x) \cos(2x)^{2}\bigr)}}
        {(1) \bigl(64e^{4x}\sin(2x)^{4} + 128e^{4x}\sin(2x)^{2}\cos(2x)^{2} + 64e^{4x}
                \cos(2x)^{4}\bigr)}
                \, dx  \\
    &= - \int
        \frac{(-48x^{2} - 16x -12)e^{4x}\cos(2x)^{2} + 8e^{4x}\sin(2x)\cos(2x) + 12e^{4x}}{64e^{4x} \, dx}
    \end{aligned}
        }
\]
    \end{adjustwidth}

Addendum: one more possibilities, with normal font size for all equations:

enter image description here

\documentclass{article}
\usepackage{mathtools}

\usepackage{lipsum}

\begin{document}
\lipsum[1][1-2]
    \begin{align*}
U_1 &= (-1)^{4-1} \int{ \frac{F(x) W_1(x)}{a W(x)} \, dx}
\intertext{where are:}
    &  \begin{aligned}
        W_1(x) & = e^{x} (2+6x) \cos(2x) + 3 \sin(2x),  \\
        W_1(x) & = \begin{multlined}[t]
                    -8 e^{3x}\sin(2x)^{2}\cos(2x)x - 8 e^{3x}\cos(2x)^{3} x +4 e^{3x} + {}\\
                        \sin(2x)^{3} + 4e^{3x}\sin(2x)\cos(2x)^{2}
                    \end{multlined}                     \\
        \alpha & =1                                     \\
        W(x)   & = 64e^{4x}\sin(2x)^{4} + 
                    128e^{4x}\sin(2x)^{2}\cos(2x)^{2} + 64e^{4x}\cos(2x)^{4}\bigr)
        \end{aligned}   
\intertext{which yields to:}
    &= - \int
        \frac{(-48x^{2} - 16x -12)e^{4x}\cos(2x)^{2} + 8e^{4x}\sin(2x)\cos(2x) + 12e^{4x}}
             {64e^{4x}} \, dx
    \end{align*}
\lipsum[1][3-4]
\end{document}
2
  • 1
    Thanks for the solution. The problem is that using \begin{adjustwidth}{-\marginparwidth}{-\marginparwidth} makes all the equations get affected by this, whether it is needed or not. The issue is, I do not know when to use this mechanism you suggested. This is all done in a program. I do not get to look at each equation generated and decide, and do not know what code is needed to let me decide if the equation is too long or not at run-time. If I apply this to each equations, even to \\[ x=y \\] then it will not work. But with the scaling option, it kicks in, only when needed. Thanks.
    – Nasser
    Oct 25, 2020 at 9:00
  • @Nasser, that's your opinion. To my taste provided solution is consistent and gives moderate nice result. However, you just stick to compression of equation(s) to unreadable small fonts ... That is up to you : I would never do this.
    – Zarko
    Oct 25, 2020 at 9:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .