# Problem with LaTeX format

I am trying to write the following function via latex

$$$$u(x,t)=\begin{cases} \frac{x^2}{2}+(t-x) ,\quad 0t>0 \end{cases}$$$$

and gives a result

What do I have to do to fix the format?

• Can you explain how this is linked to Mathematica and, if it is, can you give the MMA code you are using? Commented Jun 28, 2023 at 14:41
• @MarcoB of course. I am writing a Mathematica notebook where I solve a PDE. First I want to write the mathematical solution and then the Mathematica code that gives us the 3D-PLOT. u(x,t)=\begin{cases} \frac{x^2}{2}+(t-x) ,\quad 0<x<t \\ xt-\frac{t^2}{2}+sin(x-t),\quad x>t>0 \end{cases}. and this is the code I have written in my notebook Commented Jun 28, 2023 at 14:52
• Mathematica (13.2) doesn't seem to recognize the array syntax (even using double backslashes as per link). However for more simple expressions, it does. See this post: mathematica.stackexchange.com/questions/2128/…
– josh
Commented Jun 28, 2023 at 15:22
• Why not write it as Mathematica expression and then use TeXForm? I do not know really know what the issue here where you say I am trying to write the following function via latex ? Why write Latex directly when Mathematica can generate it for you? Or do you mean you had this Latex from some where else and wanted to convert it to Mathematica expression? Commented Jun 28, 2023 at 17:40
• @Nasser: Believe OP wishes to latex-format the case or array expression in a notebook cell such as a Display Formula cell using for example Insert/Inline Tex input into a cell.
– josh
Commented Jun 28, 2023 at 20:50

Believe OP wishes to latex-format the case or array expression in a notebook cell such as a Display Formula cell using for example Insert/Inline Tex input into a cell

In this case, why not use this:

u[x_,t_]:=Piecewise[{{x^2/2+(t-x),0<x<t},{x t-t^2/2+Sin[x-t],x>t>0}}]
tex=TeXForm[u[x,t]];
latex=MaTeX[tex,Magnification->2];
CellPrint[TextCell[Row[{"my formula is ",latex}],"Text"]]


CellPrint[TextCell[Row[{"The latex for the above is\n",tex}],"Text"]]


I still not sure what the problem is. But if they have input as Latex and want to convert that to Mathematica Expression, (in this case to Piecewise) then that is different issue.

Not all Latex can be converted back to the original Mathematica expression which generated the Latex (may be it should?). There are limitations.

 ToExpression[ToString@tex,TeXForm]


• Bit of overhead configuring MaTeX and pointing it to GhostScript and pdfLatex.exe if OP is interested in this approach. Here's an improvement if I'm interpreting what OP wants: It centers it and includes the "u(x,t)=" prefix: tex = TeXForm[ HoldForm@(Piecewise[{{x^2/2 + (t - x), 0 < x < t}, {x t - t^2/2 + Sin[x - t], x > t > 0}}])]; newTex = "u(x,t)=" <> ToString@tex; latex = MaTeX[newTex, Magnification -> 1.5]; CellPrint[TextCell[Row[{latex}], TextAlignment -> Center]]
– josh
Commented Jun 28, 2023 at 21:53
• Thank you very much both of you for your responses. I have tried the format I have used in Overleaf. That does not work. Yours is pretty handy, Commented Jun 29, 2023 at 2:41
u[x_, t_] :=
Which[0 < x < t, x^2/2 + (t - x),
x > t > 0, x t - t^2/2 + Sin[x - t]]

Plot3D[u[x, t],
{x, 0, 5}, {t, 0, 5}]