0

I have an issue in a system I am working on where long equations go beyond the page width. The LaTeX is automatically generated by an algorithm and I would like to have a way to make it fit in the page even if that makes it a little smaller. This solution would be fine for cases where the equation is a little larger than the page width. Obviously for extreme cases the developer will have to split the equation into more equations.

In my estimate it is way harder to make the system automatically recognise positions in an equation where it could split it in more lines.

After going through various posts in this forum (for example this one can i use resize box in align) I have not found a working sample of code using \resizebox in an align environment. How could I resize the following equation for example to fit the page?

\begin{align*}
    N_{Ed0} &= N_x · \cos^2(\phi_0) + N_y · \sin^2(\phi_0) + 2 · N_{xy} · \sin(\phi_0) · \cos(\phi_0) - N_0 · \cot(\phi_0) \\
            &= (-1.81234E3) · \cos^2(47.0113) + (-9.06168E3) · \sin^2(47.0113) + 2 · 28.19E3 · \sin(47.0113) · \cos(47.0113) - 18.4356 · \cot(47.0113) = \mathbf{ 22.4121E3 \: N }
\end{align*}

PS: The decimal points are a user option so the solution could not be "reduce the decimals".

7
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Jul 20, 2023 at 8:39
  • \resizebox{\textwidth}{!}{$\begin{aligned}.....$} Commented Jul 20, 2023 at 8:49
  • @DavidCarlisle Is it possible to do that with align*, or make it so that aligned shows the same result as align*? From what I gather aligned requires \begin{equation} which in turn shows equation numbering which I don't want. Have I got something wrong?
    – Tsaras
    Commented Jul 20, 2023 at 10:09
  • aligned will show the same result as align*. But aligned is set in hmode so can be scaled, and align* is a vertical display construct that you can not put in an hbox Commented Jul 20, 2023 at 10:11
  • 1
    Why would you put it in equation??? Just use what I wrote. Commented Jul 20, 2023 at 10:11

2 Answers 2

3

You need a horizontal mode construct in \resizebox I really can not recommend doing this at all as it quickly makes the math unreadable with no warning but:

enter image description here

\documentclass{article}

\usepackage{amsmath,graphicx}
\DeclareUnicodeCharacter{00B7}{\cdot}
\begin{document}
\noindent X\dotfill X
\begin{center}
\resizebox{\textwidth}{!}{$\begin{aligned}
    N_{Ed0} &= N_x · \cos^2(\phi_0) + N_y · \sin^2(\phi_0) + 2 · N_{xy} · \sin(\phi_0) · \cos(\phi_0) - N_0 · \cot(\phi_0) \\
            &= (-1.81234E3) · \cos^2(47.0113) + (-9.06168E3) · \sin^2(47.0113) + 2 · 28.19E3 · \sin(47.0113) · \cos(47.0113) - 18.4356 · \cot(47.0113) = \mathbf{ 22.4121E3 \: N }
\end{aligned}$}
\end{center}
\end{document}

I would drop the alignment and let latex break the inline math with no font scaling

enter image description here

\usepackage{amsmath,graphicx}
\DeclareUnicodeCharacter{00B7}{\cdot}
\begin{document}
\noindent X\dotfill X
\begin{center}
$
    N_{Ed0} = N_x · \cos^2(\phi_0) + N_y · \sin^2(\phi_0) + 2 · N_{xy} · \sin(\phi_0) · \cos(\phi_0) - N_0 · \cot(\phi_0) $\\$
            = (-1.81234E3) · \cos^2(47.0113) + (-9.06168E3) · \sin^2(47.0113) + 2 · 28.19E3 · \sin(47.0113) · \cos(47.0113) - 18.4356 · \cot(47.0113) = \mathbf{ 22.4121E3 \: N }
$
\end{center}
\end{document}
6
  • Your second suggestion is very interesting, although it would be better with alignment. Does Latex automatically break the equation in appropriate locations? Meaning if there is a denominator with a lot of + and - in \frac for example, it will not break the fraction.
    – Tsaras
    Commented Jul 20, 2023 at 10:55
  • 1
    @Tsaras it can only break math at top level mathbin or mathrel operators not inside any {} group and definitely not inside a fraction Commented Jul 20, 2023 at 10:56
  • This has all been very helpful, thank you very much. One out of topic question, which however is very relevant for my situation: is there a way to measure if an equation would fit in the page, in which case to use the align* environment that looks better, or if it wouldn't, let it break the math without alignment?
    – Tsaras
    Commented Jul 20, 2023 at 11:12
  • 1
    @Tsaras replace \resizebox{\textwidth}{!} by \sbox{0} so you grab it all in box 0 then you can use \ifdim\wd0>\textwidth ... \else ... \fi Commented Jul 20, 2023 at 11:17
  • @Tsaras you could also try breqn package if you are feeling brave Commented Jul 20, 2023 at 11:18
1

Here's a possible solution, but this will lead to very poor typesetting.

The argument to scaleddisplay is the type of environment you want to scale and can be equation, gather, align or alignat.

\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{newunicodechar}

\usepackage{lipsum}% for mock text

\newunicodechar{·}{\TextOrMath{\textperiodcentered}{\cdot}}

\ExplSyntaxOn

\NewDocumentEnvironment{scaleddisplay}{mb}
 {% #1 = type of alignment (equation,gather,align,alignat)
  % #2 = body
  \tsaras_scaleddisplay:nn { #1 } { #2 }
 }{}

\cs_new_protected:Nn \tsaras_scaleddisplay:nn
 {
  \begin{displaymath}
  \resizebox{\textwidth}{!}
   {
    \__tsaras_scaleddisplaytype:nn { #1 } { #2 }
   }
  \end{displaymath}
 }

\cs_new:Nn \__tsaras_scaleddisplaytype:nn
 {
  \str_case:nn { #1 }
   {
    {equation}{$#2$}
    {gather}{$\begin{gathered}#2\end{gathered}$}
    {align}{$\begin{aligned}#2\end{aligned}$}
    {alignat}{$\begin{alignedat}{-1}#2\end{alignedat}$}
   }
 }

\ExplSyntaxOff

\begin{document}

\lipsum[1][1-4]
\begin{scaleddisplay}{align}
    N_{Ed0} &= N_x · \cos^2(\phi_0) + N_y · \sin^2(\phi_0) + 2 · N_{xy} · \sin(\phi_0) · \cos(\phi_0) - N_0 · \cot(\phi_0) \\
            &= (-1.81234E3) · \cos^2(47.0113) + (-9.06168E3) · \sin^2(47.0113) + 2 · 28.19E3 · \sin(47.0113) · \cos(47.0113) - 18.4356 · \cot(47.0113) = \mathbf{ 22.4121E3 \: N }
\end{scaleddisplay}
\lipsum[2][1-4]

\end{document}

enter image description here

You must log in to answer this question.

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