2
$\begingroup$

I am trying to write the following function via latex

\begin{equation} 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} \end{equation}

and gives a result enter image description here

What do I have to do to fix the format?

$\endgroup$
6
  • 1
    $\begingroup$ Can you explain how this is linked to Mathematica and, if it is, can you give the MMA code you are using? $\endgroup$
    – MarcoB
    Commented Jun 28, 2023 at 14:41
  • $\begingroup$ @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 $\endgroup$ Commented Jun 28, 2023 at 14:52
  • 1
    $\begingroup$ 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/… $\endgroup$
    – josh
    Commented Jun 28, 2023 at 15:22
  • 2
    $\begingroup$ 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? $\endgroup$
    – Nasser
    Commented Jun 28, 2023 at 17:40
  • 1
    $\begingroup$ @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. $\endgroup$
    – josh
    Commented Jun 28, 2023 at 20:50

2 Answers 2

3
$\begingroup$

Replying to comment:

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"]]

Mathematica graphics

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

Mathematica graphics

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]

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ 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]] $\endgroup$
    – josh
    Commented Jun 28, 2023 at 21:53
  • $\begingroup$ 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, $\endgroup$ Commented Jun 29, 2023 at 2:41
2
$\begingroup$
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}]

enter image description here

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.