Skip to content

Yurie/MathForm

A Mathematica paclet for improving math layout.

  • The LaTeX template file Source/template is from MaTeX.

  • The exported LaTeX strings will be formatted by tex-fmt.

Flow chart


flowchart LR

Expression(Expression)
TraditionalForm(TraditionalForm)
TeXForm(TeXForm)
LaTeXString(LaTeXString)
PDF(PDF)

$MFAssoc($MFAssoc)

MFInterpret{{MFInterpret}}
MFString{{MFString/MFCopy}}
MFArgConvert{{MFArgConvert}}
MFClear{{MFClear}}
MF{{MF}}

subgraph dataflow[" "]
Expression --> TraditionalForm --> TeXForm
TeXForm --- MFString --> LaTeXString
LaTeXString --- MF --> PDF
end

MFArgConvert --> $MFAssoc
MFClear -->|cancel| MFInterpret & MFArgConvert
MFInterpret --> TraditionalForm
$MFAssoc --> MFString

classDef data fill:#ffc6ec20;
class MFInterpret,MFString,MFArgConvert,MFClear,$MFAssoc,MF data;

style dataflow fill:#ffdaaa20,stroke-width:0,rx:1rem,ry:1rem

Example

MFArgConvert[]@{
    f[_],
    args[___]->"g",
    argList[_List]->"h"
}
args[f[x],f[y]]//MFString

argList[{f[x],f[y]}]//MFString
\g{\f{x}}{\f{y}}

\h{
    \f{x},
    \f{y}
}

Formatting

  • MFString - refine the string from TeXForm.

    The expression will be converted to string by TeXForm, then refined according to $MFAssoc and the following options:

    • "RemoveLeftRightPair"->True - remove the \left(...\right) pair.

    • "Linebreak"->True - insert line breaks into long expressions.

    • "LinebreakThreshold"->6 - control the threshold of line breaking.

    • "LinebreakIgnore"->{} - treat the sub-expressions matching with the specified patterns as symbols.

  • MFCopy - copy the string from MFString and return the original expression.

    • The options are inherited from MFString.
  • MF - show the LaTeX of the expression.

    The expression will be converted to LaTeX string by MFString, then compiled into PDF, controlled by the following options:

    • "Preamble"->{"\\usepackage{amsmath,amssymb}"} - add to the preamble.

    • "FontSize"->12 - adjust the font size.

    • "LineSpacing"->{1.2,0} - adjust the line spacing.

    • "Magnification"->1.5 - magnify the output.

    • "CopyToClipboard"->True - copy the string from MFString to clipboard.

    • "ClearCache"->False - use the cache.

    • "Listable"->True - convert list elements as separate PDFs.

    • The other options are inherited from MFString.

Format definition

  • MFInterpret - set interpretable format values.

    Usage

    MFInterpret[Format,format_.,{pattern_,definition_,interpretation_.}]
    
    MFInterpret[MakeBoxes,format_.,{pattern_,definition_,interpretation_.}]
    
    MFInterpret[__,{__List}] (*batch-operation*)
    
    • The arguments format_ and interpretation_ can be omitted.

    • The supported formats are StandardForm|TraditionalForm or non-specified.

  • MFArgConvert - define LaTeX macro for the symbol and store the rule into $MFAssoc.

    Example

    MFArgConvert[][f->"f"]
    
    MFArgConvert["{","}"][f[_]->"f"]
    
    MFArgConvert["{","}"][f[___]->"f"]
    
    MFArgConvert["{","}",","][f[_List]->"f"]
    
    MFArgConvert["{","}",","][f[___List]->"f"]
    
    MFArgConvert[___][{__}] (*batch-operation*)
    
    • The existing format values will be cleared and messaged, since the symbol name is used as the key in $MFAssoc.

    • The supported conversion rules are as follows:

      Pattern Expression LaTeX
      f f \f
      f[_] f[a] \f{a}
      f[___] f[a,b] \f{a}{b}
      f[_List] f[{a,b}] \f{a,b}
      f[___List] f[{a,b},{c,d}] \f{a,b}{c,d}

  • MFClear - clear format values and rules in $MFAssoc of the symbol, or all symbols under the context.