# Really big MATLAB equation to be put into LaTeX [closed]

I have a very big equation in MATLAB

f1=((y_o2_ca_in*M_o2/(y_o2_ca_in*M_o2+(1-y_o2_ca_in)*M_n2))*(((k_sm_out*(x5-((x1*R_o2+x3*R_n2+x8*R_v)*t_st/v_ca)))+((M_v/M_a)*phi_des*(((10^(-1.69*10^-10*t_cl^4+3.85*10^-7*t_cl^3-3.39*10^-4*t_cl^2+0.143*t_cl-20.92))*10^3)/(x5-(((x5*phi_atm*p_sat_t_atm)/(p_atm*p_sat_t_cl))*p_sat_t_cl)))*((1/(1+((M_v/M_a)*((((x5*phi_atm*p_sat_t_atm)/(p_atm*p_sat_t_cl))*p_sat_t_cl)/(x5-(((x5*phi_atm*p_sat_t_atm)/(p_atm*p_sat_t_cl))*p_sat_t_cl))))))*(k_sm_out*(x5-((x1*R_o2+x3*R_n2+x8*R_v)*t_st/v_ca))))-((k_sm_out*(x5-((x1*R_o2+x3*R_n2+x8*R_v)*t_st/v_ca)))-((1/(1+((M_v/M_a)*((((x5*phi_atm*p_sat_t_atm)/(p_atm*p_sat_t_cl))*p_sat_t_cl)/(x5-(((x5*phi_atm*p_sat_t_atm)/(p_atm*p_sat_t_cl))*p_sat_t_cl))))))*(k_sm_out*(x5-((x1*R_o2+x3*R_n2+x8*R_v)*t_st/v_ca)))))))/(1+((M_v/(y_o2_ca_in*M_o2+(1-y_o2_ca_in)*M_n2))*(((x5*0.5*p_sat_t_atm/(p_atm*p_sat_t_cl))*((10^(-1.69*10^-10*t_ca_in^4+3.85*10^-7*t_ca_in^3-3.39*10^-4*t_ca_in^2+0.143*t_ca_in-20.92))*10^3))/(((x5-(((x5*phi_atm*p_sat_t_atm)/(p_atm*p_sat_t_cl))*p_sat_t_cl))+phi_des*((10^(-1.69*10^-10*t_cl^4+3.85*10^-7*t_cl^3-3.39*10^-4*t_cl^2+0.143*t_cl-20.92))*10^3))-((x5*0.5*p_sat_t_atm/(p_atm*p_sat_t_cl))*((10^(-1.69*10^-10*t_ca_in^4+3.85*10^-7*t_ca_in^3-3.39*10^-4*t_ca_in^2+0.143*t_ca_in-20.92))*10^3))))))))-((((x1*R_o2*t_st/v_ca)/((x1*R_o2*t_st/v_ca)+(x3*R_n2*t_st/v_ca)))*M_o2/(((x1*R_o2*t_st/v_ca)/((x1*R_o2*t_st/v_ca)+(x3*R_n2*t_st/v_ca)))*M_o2+(1-((x1*R_o2*t_st/v_ca)/((x1*R_o2*t_st/v_ca)+(x3*R_n2*t_st/v_ca))))*M_n2))*(1/(1+((M_v/(((x1*R_o2*t_st/v_ca)/((x1*R_o2*t_st/v_ca)+(x3*R_n2*t_st/v_ca)))*M_o2+(1-((x1*R_o2*t_st/v_ca)/((x1*R_o2*t_st/v_ca)+(x3*R_n2*t_st/v_ca))))*M_n2))*((x8*R_v*t_st/v_ca)/((x1*R_o2*t_st/v_ca)+(x3*R_n2*t_st/v_ca)))))*(k_ca_out*(((x1*R_o2+x3*R_n2+x8*R_v)*t_st/v_ca)-x9))))-((M_o2*n*i_st)/(4*F));


Which I want to put it in LaTeX. I have tried everything like using lstlisting, equation splitting etc, but it's too big to put into LaTeX. Requesting your guidance/help on this.

-

## closed as unclear what you're asking by Martin Schröder, Jesse, Zarko, ChrisS, PapiroMar 7 at 14:41

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

do you want it to look like code or it can be math notation (e.g. fractions)? – alfC Mar 7 at 4:27
Expressions such as p_sat_t_atm and p_sat_t_cl will necessarily throw "double subscript" errors. How should (La)TeX interpret the constituent terms of these expressions? Separately, should terms such as phi be printed literally, or should the be printed as \phi, i.e., using the Greek lowercase letter? – Mico Mar 7 at 9:12

Here's my manual approach to untangling and manually wrapping the code:

\documentclass{article}
\usepackage{showframe}
\usepackage{listings}

\begin{document}
\begin{lstlisting}[language=Matlab,gobble=8]
a = x1*R_o2*t_st/v_ca;
b = x3*R_n2*t_st/v_ca;
c = x1*R_o2+x3*R_n2+x8*R_v;
d = x5*phi_atm*p_sat_t_atm/(p_atm*p_sat_t_cl)*p_sat_t_cl;
e = (10^(-1.69e-10*t_cl^4
+3.85e-07*t_cl^3
-3.39e-04*t_cl^2
+1.43e-01*t_cl
-20.92))*10^3;
f = x5*0.5*p_sat_t_atm/(p_atm*p_sat_t_cl)*e;
g = y_o2_ca_in*M_o2+(1-y_o2_ca_in)*M_n2;
h = c*t_st/v_ca;
i = k_sm_out*(x5-h);

j = (M_v/M_a)*phi_des*(e/(x5-d));
* ((1/(1+((M_v/M_a)*(d/(x5-d)))))*i);
k = i-(1/(1+((M_v/M_a)*(d/(x5-((dtm)/(p_atm*p_sat_t_cl))
*p_sat_t_cl)))))*i;
l = (a/(a+b))*M_o2/((a/(a+b))*M_o2+(1-(a/(a+b)))*M_n2);
m = (M_v/((a/(a+b))*M_o2+(1-(a/(a+b)))*M_n2))
* ((x8*R_v*t_st/v_ca)/(a+b));

f1 = y_o2_ca_in*M_o2/g*(i+(j-k))/(1+((M_v/g)*(f/(e-f))))
- l*(1/(1+m)*(k_ca_out*(h-x9)))
- ((M_o2*n*i_st)/(4*F));
\end{lstlisting}
\end{document}


Variables a to i are subexpressions that occur more than once in the original assignment. Variables j to m occur only once, but have been introduced to reduce the length of the final assignment. Of course, it would be great to find sensible names for these variables, but without any idea what this equation does, this is impossible.

This solution is actually shorter (both in terms of characters and lines) than automatically wrapping the original equation. Should you decide to use it, please verify that I didn't introduce any errors!

[EDIT] And finally, here's a math-ified version of the above:

\documentclass{article}
\usepackage{showframe}
\usepackage{amsmath}
\usepackage{mathtools}

\begin{document}
\begin{align*}
a &= x_1 R_{o_2}\cdot\frac{t_{st}}{v_{ca}}&
b &= x_3 R_{n_2}\cdot\frac{t_{st}}{v_{ca}}\\
c &= x_1 R_{o_2} + x_3 R_{n_2} + x_8 R_v&
d &= x_5 \phi_{atm}\cdot\frac{p_{sat,t,atm}}{p_{atm}}\\
e &= \mathrlap{10^{3 - 1.69\cdot10^{-10}\cdot t_{cl}^4 + 3.85\cdot10^{-7}\cdot t_{cl}^3 - 3.39\cdot10^{-4}\cdot t_{cl}^2 + 1.43\cdot10^{-1}\cdot t_{cl} -20.92}}\\
f &= \frac{x_5}2\cdot\frac{p_{sat,t,atm}}{p_{atm}\cdot p_{sat,t,cl}}\cdot e&
g &= y_{o_2,ca,in}\cdot M_{o_2} + 1 - y_{o_2,ca,in}\cdot M_{n_2}\\
h &= c \cdot \frac{t_{st}}{v_{ca}}&
i &= k_{sm,out}\cdot(x_5-h)\\
j &= \frac{M_v}{M_a}\cdot\frac{\phi_{des}\cdot e}{x_5-d}\cdot\frac{i}{1+\frac{M_v}{M_a}\cdot\frac{d}{x_5-d}}&
k &= i-\frac{i}{1+\frac{M_v}{M_a}\cdot\frac{d}{x_5-\frac{dtm}{p_{atm}}}}\\
l &= \mathrlap{\frac{a}{a+b}\cdot \frac{M_{o_2}}{\frac{a}{a+b}\cdot M_{o_2} + \left(1-\frac{a}{a+b}\right)\cdot M_{n_2}}}\\
m &= \mathrlap{\frac{M_v}{\frac{a}{a+b}\cdot M_{o_2}+\left(1-\frac{a}{a+b}\right)\cdot M_{n_2}}\cdot\frac{x_8 R_v\cdot\frac{t_{st}}{v_{ca}}}{a+b}}\\
f_1 &= \mathrlap{\frac{y_{o_2,ca,in}\cdot\frac{M_{o_2}}{g}\cdot(i+j-k)}{1+\frac{M_v}{g}\cdot\frac{f}{e-f}} - l\cdot\frac1{1+m}\cdot k_{ca,out}\cdot(h-x_9) - \frac{M_{o_2}\cdot n\cdot i_{st}}{4F}}
\end{align*}
\end{document}


I've used two columns to display the equations to save some space with the shorter ones, and used \mathrlap{} from mathtools to make the longer equations span multiple columns.

Maybe it's something to work with, although it's certainly not pretty all by itself.

-

You can always use lstlisting's breaklines option:

\documentclass{article}
\usepackage{showframe}
\usepackage{listings}

\begin{document}
\begin{lstlisting}[language=matlab,breaklines,gobble=8]
f1=...;
\end{lstlisting}
\end{document}


This of course doesn't magically make the code readable. It looks as if it were auto-generated, but maybe it can be broken into separate statements by extracting some common terms.

Otherwise, I'd suggest including a link to an on-line version or maybe a QR code so readers can use their favorite editor to dissect the code.

-

Unlike with text, in equations you need to manually make hyphenation. For this you can use \begin{split}, from amsmath package. Here is simple code, to show how it works

\documentclass{article}
\usepackage{amsmath}

\begin{document}

$\begin{split} f(x)&=x^2+y\\ &y+8\\ f(x)g(x)&=x^3 \end{split}$

\end{document}


$...$ is like $$...$$, but better.

-