Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-ASCII characters math replacements #17

Merged
merged 5 commits into from
May 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 153 additions & 8 deletions CellsToTeX/CellsToTeX.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
contains converted cell contents and data extracted from Cell options."


CellsToTeXPreamble::usage =
"\
CellsToTeXPreamble[] \
returns String with TeX code setting global properties of mmacells package, \
suitable for inclusion in document preamble."


CellsToTeXException::usage =
"\
CellsToTeXException \
Expand Down Expand Up @@ -142,6 +149,20 @@
or of symbol with given \"name\"."


texMathReplacement::usage =
"\
texMathReplacement[\"str\"] \
returns String with TeX code representing given String \"str\". By default \
DownValues of texMathReplacement are used by CellsToTeXPreamble."


texMathReplacementRegister::usage =
"\
texMathReplacementRegister[\"str\"] \
defines TeX code math replacement for given String \"str\", if it's not \
already defined. Returns \"str\"."


makeString::usage =
"\
makeString[boxes] \
Expand Down Expand Up @@ -1456,11 +1477,15 @@ cell form (String or None)."
(*optionValueToTeX*)


optionValueToTeX[True] = "true"

optionValueToTeX[False] = "false"

optionValueToTeX[subOpts:{OptionsPattern[]}] := optionsToTeX["{", subOpts, "}"]

optionValueToTeX[val_] :=
With[{str = ToString[val]},
If[StringTake[str, 1] === "{" && StringTake[str, -1] === "}" ||
StringFreeQ[str, {"[", "]", ",", "="}]
,
If[StringMatchQ[str, "{*}"] || StringFreeQ[str, {"[", "]", ",", "="}],
str
(* else *),
"{" <> str <> "}"
Expand All @@ -1472,13 +1497,14 @@ cell form (String or None)."
(*optionsToTeX*)


optionsToTeX[keyval:{(Rule | RuleDelayed)[_String, _]...}] :=
StringJoin[Riffle[(#1 <> "=" <> optionValueToTeX[#2]) & @@@ keyval, ","]]
optionsToTeX[keyval:{OptionsPattern[]}] :=
StringJoin @
Riffle[(ToString[#1] <> "=" <> optionValueToTeX[#2]) & @@@ keyval, ","]

optionsToTeX[_String, {}, _String] := ""

optionsToTeX[
pre_String, keyval:{(Rule | RuleDelayed)[_String, _]...}, post_String
pre_String, keyval:{OptionsPattern[]}, post_String
] :=
pre <> optionsToTeX[keyval] <> post

Expand Down Expand Up @@ -1917,6 +1943,22 @@ cell form (String or None)."
defaultAnnotationType[_Symbol | _String] := "UndefinedSymbol"


(* ::Subsubsection:: *)
(*texMathReplacementRegister*)


texMathReplacementRegister[str_String] := (
If[! StringQ @ texMathReplacement[str],
With[{texCode = StringTrim @ System`Convert`TeXFormDump`MakeTeX[str]},
If[texCode =!= "",
texMathReplacement[str] = texCode
]
]
];
str
)


(* ::Subsubsection:: *)
(*charToTeX*)

Expand Down Expand Up @@ -2478,8 +2520,11 @@ cell form (String or None)."
(*Common operations*)


Protect @ Evaluate @ Names[
"CellsToTeX`Configuration`" ~~ Except["$"] ~~ Except["`"]...
Protect @ Evaluate @ DeleteCases[
Names["CellsToTeX`Configuration`" ~~ Except["$"] ~~ Except["`"] ...],
"texMathReplacement",
{1},
1
]


Expand Down Expand Up @@ -2560,6 +2605,106 @@ cell form (String or None)."
]


(* ::Subsubsection:: *)
(*CellsToTeXPreamble*)


Options[CellsToTeXPreamble] = {
"Gobble" -> Automatic,
"UseListings" -> Automatic,
"TeXOptions" -> {},
"TeXMathReplacement" -> texMathReplacement,
"CatchExceptions" -> True
}


functionCall:CellsToTeXPreamble[OptionsPattern[]] :=
If[OptionValue["CatchExceptions"],
catchException
(* else *),
Identity
] @ Module[
{gobble, useListings, texOptions, mathReplacement}
,
{gobble, useListings, texOptions, mathReplacement} =
OptionValue @
{"Gobble", "UseListings", "TeXOptions", "TeXMathReplacement"};

If[!MatchQ[texOptions, {OptionsPattern[]}],
throwException[functionCall,
{"Unsupported", "OptionValue", "TeXOptions"},
{texOptions, {"list of options"}}
]
];
If[!MatchQ[mathReplacement, Except[HoldPattern@Symbol[___], _Symbol]],
throwException[functionCall,
{"Unsupported", "OptionValue", "TeXMathReplacement"},
{mathReplacement, {"a symbol"}}
]
];

(* By default gobble default indentation of mmaCell. *)
If[gobble === Automatic,
gobble =
StringLength @ OptionValue[mmaCellProcessor, "Indentation"]
];
Switch[gobble,
_Integer?NonNegative,
PrependTo[texOptions, "morefv" -> {"gobble" -> gobble}]
,
Except[None],
throwException[functionCall,
{"Unsupported", "OptionValue", "Gobble"},
{gobble, {Automatic, None, "non-negative integer"}}
]
];

(* Listings are used to highlight non-annotated code using TeX
environment options. If moving annotations to options is switched
off then, by default, don't use listings, otherwise use them.
Since listings are, by default, switched on in mmacells package,
if we're using them, we can just omit this option. *)
If[useListings === Automatic,
useListings =
Replace[
OptionValue[
annotateSyntaxProcessor,
"CommonestTypesAsTeXOptions"
],
Except[False] -> None
]
];
Switch[useListings,
True | False,
PrependTo[texOptions, "uselistings" -> useListings]
,
Except[None],
throwException[functionCall,
{"Unsupported", "OptionValue", "UseListings"},
{useListings, {Automatic, True, False, None}}
]
];
StringJoin @ Riffle[
DeleteCases[
Prepend[
StringJoin[
"\\mmaDefineMathReplacement{",
#1[[1, 1]],
"}{",
#2,
"}"
]& @@@
DownValues @ Evaluate[mathReplacement]
,
optionsToTeX["\\mmaSet{", texOptions, "}"]
],
""
],
"\n"
]
]


(* ::Subsubsection:: *)
(*Common operations*)

Expand Down
41 changes: 41 additions & 0 deletions CellsToTeX/Tests/Integration/CellToTeX.mt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,47 @@ Block[{\[Phi]1},
]


Block[{texMathReplacement},
texMathReplacement["\[UnderBracket]"] = "\\textvisiblespace";
UsingFrontEnd @ Test[
CellToTeX[
{
\[Alpha]_ \[RuleDelayed] \[Alpha],
x \[Equal] \[Beta]\[UnderBracket]\[Gamma]
} // MakeBoxes
,
"Style" -> "Code",
"ProcessorOptions" -> {
"NonASCIIHandler" -> texMathReplacementRegister,
"CharacterEncoding" -> "Unicode"
}
]
,
"\
\\begin{mmaCell}{Code}
{\\mmaPat{\[Alpha]_} :> \\mmaPat{\[Alpha]}, x == \\mmaUnd{\[Beta]\[UnderBracket]\[Gamma]}}
\\end{mmaCell}"
,
TestID -> "pure boxes: syntax, non-ASCII symbols (private Unicode): \
Code, Unicode encoding, math replacements"
];
Test[
texMathReplacement // DownValues
,
{
HoldPattern@texMathReplacement@"\[Alpha]" :> "\\alpha",
HoldPattern@texMathReplacement@"\[Beta]" :> "\\beta",
HoldPattern@texMathReplacement@"\[Gamma]" :> "\\gamma",
HoldPattern@texMathReplacement@"\[UnderBracket]" :>
"\\textvisiblespace"
}
,
TestID -> "pure boxes: syntax, non-ASCII symbols (private Unicode): \
Code, Unicode encoding, math replacements: texMathReplacement"
]
]


Test[
CellToTeX[
BoxData @ RowBox[{
Expand Down
Loading