9
$\begingroup$

I have a little problem, and hope, with your help, I'll be able to solve it.

As you got from the title, I have some trouble with TeX in Mathematica. Imagine, I want to output a nice DisplayFormula, such that it would represent a function, which evaluates piecewise. In TeX it would look like this (Let's take a uniform distribution for example)

$$\left\{ \begin{aligned} &0,& t\leq x_{\min} \\ &\frac{t-x_{\min}}{x_{\max}-x_{\min}},&x_{\min}<t\leq x_{\max} \\ &1,& t>x_{\max} \end{aligned} \right.$$

So what's the best way to create that exact output in Mathematica?

I've tried TraditionalForm[CDF[UniformDistribution[{x_ {min}, x_ {max}}], t]], but that gives me almost the same output, but without the zero part.

enter image description here

I do know, it's not significant whether to write or not the 0 part in the piecewise function.

So it works well for CDF, but with PDF there's an extraneous part is shown at the end

TraditionalForm[CDF[UniformDistribution[{x_ {min}, x_ {max}}], t]]

enter image description here

So in the CDF Mathematica's decided not to output the 0 part, but in the PDF it's shown.

I've also tried to use

TraditionalForm[
  Piecewise[
   {{0, t <= Subscript[x, min]},
    {(t - Subscript[x, min])/(Subscript[x, max] - Subscript[x, min]), 
     Subscript[x, min] < t <= Subscript[x, max]},
    {1, t > Subscript[x, max]}},
   t
  ]
]

But that variant also reveals the part that I haven't specified.

enter image description here

The last thing I've tried to use is ToExpression["TeX source",TeXForm], but this one can't parse the simple TeX source with aligned environment (or I haven't found a way to make it work).

$\endgroup$

2 Answers 2

9
$\begingroup$

If you just want the output, well, write it manually. EscpwEsc gives you the piecewise bracket. Then you can insert a table (Insert->Table/Matrix) or learn the shortcuts with Ctrl, and CtrlReturn, etc.

I got this box structure. You can see the result by running.

RawBoxes@FormBox[
  RowBox[{"\[Piecewise]", 
    GridBox[{{RowBox[{"0", ","}], 
       RowBox[{"t", "\[LessEqual]", 
         SubscriptBox["x", "min"]}]}, {RowBox[{FractionBox[
          RowBox[{"t", "-", SubscriptBox["x", "min"]}], 
          RowBox[{SubscriptBox["x", "max"], "-", 
            SubscriptBox["x", "min"]}]], ","}], 
       RowBox[{SubscriptBox["x", "min"], "<", "t", "\[LessEqual]", 
         SubscriptBox["x", "max"]}]}, {RowBox[{"1", ","}], 
       RowBox[{"t", ">", SubscriptBox["x", "max"]}]}}]}], 
  TraditionalForm]

You can then style it further to suit you...

$\endgroup$
1
  • $\begingroup$ You're correct! Thank you. $\endgroup$ Commented Apr 23, 2012 at 6:11
6
$\begingroup$

You could use the \[AligmentMarker] character. To help you better write equations, lets forget about the DisplayFormula style and lets create our own.

Set up:

Let us create the style DisplayMath. In your notebook where you are writing your displayed equation go to Format > Edit Stylesheet.... Here you will create a new cell. Show the expression of the cell by going to Cell > Show Expression or by pressing commandshifte in a mac. Now enter the following in that expression:

Cell[StyleData["DisplayMath"],
    ConversionRules->{
        "TeX" -> {"$$\n", Convert`TeX`BoxesToTeX[#]& , "\n$$"}, 
        "HTML" -> {"$$\n", StringReplace[Convert`TeX`BoxesToTeX[#], {"<" -> " \\lt ", ">" -> " \\gt "}]& , "\n$$\n\n"}
    },
    DefaultFormatType->TraditionalForm,
    TextAlignment->Center,
    LanguageCategory->"Formula",
    AutoSpacing->True,
    ScriptLevel->0,
    AutoMultiplicationSymbol->False,
    SingleLetterItalics->True,
    SpanMaxSize->Infinity,
    CounterIncrements->"DisplayMath",
    FontFamily->"Times",
    FontSize->16,
    ShowInvisibleCharacters->All,
    UnderoverscriptBoxOptions->{LimitsPositioning->False},
    GridBoxOptions->{
    GridBoxAlignment->{
        "Columns" -> {{"\[AlignmentMarker]"}}, "ColumnsIndexed" -> {}, 
        "Rows" -> {{Baseline}}, "RowsIndexed" -> {}}
    }
]

This is the style that I'm using to display mathematics. Notice the use of ShowInvisibleCharacters, this makes the \[AligmentMarker] to be displayed. If you do not wish for this to be displayed when you print or in a presentation then turn it off when you use another environment. You do so by creating a cell with these contents:

Cell[StyleData["DisplayMath", "Printout"],
    ShowInvisibleCharacters->None
]

The option GridBoxOptions simply say that the grids in this style will be aligned using the \[AlignmentMarker]. Now lets type some math.

Writing Aligned Equations:

Start by creating a cell using the DisplayMath style, you can do this by pressing command0 in a mac use alt in windows and entering DisplayMath. Enter a+b+c=d. What we want now is to go one step down, we can do this by pressing controlreturn. If you did this now you should see a box right below "d", we want a box to be below the whole expression. So delete that box, select the whole expression and try again. After that enter the following: c=d-a-b. This is what you should see now:

not aligned

To make them become aligned, now we can use the \[AligmentMarker]. We want them to be aligned along the equal signs we can enter EscamEsc next to the equal signs. Now we should see this:

aligned

After you finish with a column, you can create another column by pressing control,. And repeat. Here is a display of your equation from your example:

result

To put the bracket I used what Rojo mentioned. I typed EscpwEsc at the beginning of the equation.

$\endgroup$
2
  • $\begingroup$ Nice method! How do you add equation numbers in your approach? $\endgroup$
    – yarchik
    Commented Jan 1, 2022 at 23:20
  • $\begingroup$ @yarchik Its been years since I've used Mathematica. I'm not sure if this still applies but back then I did ask a question related to equation numbers: mathematica.stackexchange.com/questions/29332/… The question was lated to LaTeX but maybe it can be combined with whatever I wrote here? $\endgroup$
    – jmlopez
    Commented Jan 14, 2022 at 21:56

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