Skip to content

Yurie/MathForm

A Mathematica paclet for improving math layout.

  • Due to this known TeXForm issue, this paclet aims to improve the StandardForm|MF outputs by occupying TraditionalForm definitions.

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

MF

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:

    • "RemoveLaTeXLRPair" -> True - remove the \left(...\right) pair.
  • MFCopy - copy the string from MFString and return the original expression.

    The options are the same as 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.

Format definition

  • MFInterpret - set interpretable format values.

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

    • The existing format values will be cleared and messaged.

    • 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.

Index

  • indexize[var_,index_]|indexize[{var_,index_}] - join the variable and index into a symbol.

    This is basically ToExpression[ToString[var_]<>ToString[index_]], but treating Null as an empty string.

    Example

    indexize[z,1]
    
    Out[] = z1
    
  • indexSplit[vars__|{vars__}][expr_] - split symbols into indexed variables in the expression.

    Example

    z1+z2//indexSplit[z]
    
    Out[] = z[1]+z[2]
    
    • "IndexPosition"->Construct - controls the format of indexed vairables.

      The supported values are

      • Construct
      • Subscript
      • Superscript

      Example

      z1+z2//indexSplit[z,"IndexPosition"->Subscript]
      
      Out[] = Subscript[z,1]+Subscript[z,2]
      
    • "IndexType"->All - controls the pattern of indices, and resolves possible conflicts between variable and index.

      The supported values are

      • All
      • "PositiveInteger"
      • "PositiveIntegerOrSingleLetter"
      • _Symbol - any function for string pattern matching

      Example

      zb1//indexSplit[{z,zb},"IndexType"->"PositiveInteger"]
      
      Out[] = zb[1]
      
      zb1//indexSplit[{z,zb}]
      
      Out[] = z[b1]
      
  • indexJoin[vars__|{vars__}][expr_] - join indexed variables into symbols in the expression.

    The options are the same as indexJoin.

    Example

    z[1]+z[2]//indexJoin[z]
    
    Out[] = z1+z2