# Special notation using round brackets and avoiding commas (for $\LaTeX$ input)?

Internally, when I work with mathematica I use a notation like br[1,2,3,4] to denote some quantities. Then I would like to export the results in TeX form and for this I need a special notation. I need to make the whole expression appear as follows:

br[1,2,3,4]$\rightarrow (1~ 2~ 3~ 4)$

So there should be small spaces but no commas between the numbers, and the whole thing should be in round brackets (which acquire a special meaning as an abbreviation for a mathematical object). I was thinking of using the Needs["Notation"] environment, but unfortunately it does not allow round brackets and does not get rid of commas. Can this be done in some other way?

EDIT

Desired $\LaTeX$ syntax example:

br[1,2,3,4]/br[2,3,4,1]+br[2,3,4,1]/br[1,2,3,4]$\rightarrow\frac{(1~2~3~4)}{(2~3~4~1)}+\frac{(2~3~4~1)}{(1~2~3~4)}$

where the latex part above is explicitly:

\frac{(1~2~3~4)}{(2~3~4~1)}+\frac{(2~3~4~1)}{(1~2~3~4)}

• Ultimately, the output should be in $\LaTeX$ format so that I can copy it over directly into a latex file and compile. If a function converts it to a string first and then to latex, it makes no difference. – Kagaratsch Sep 30 '15 at 20:23
• Oh, it kind of works with a single br[...], but the whole problem is that expressions will appear as functions of several br[...]'s. Using the above conversion to string gives the //TeXForm as \text{(1 2 3 4)}. And I would like to directly paste the output into a math environment in latex without \text{ } wrappers around each bracket. – Kagaratsch Sep 30 '15 at 20:29
• Can you show an example of the desired latex syntax? – george2079 Sep 30 '15 at 20:32
• I will edit the question to show desired latex syntax. – Kagaratsch Sep 30 '15 at 20:32
• – jkuczm Oct 1 '15 at 7:52

Here is a solution that automatically formats any instance of br[...] in the format that you want and that will be copy-and-pastable into LaTeX after converting to TexForm.

Format[br[a__]] := DisplayForm@RowBox[Riffle[ToString /@ {a}, "\[ThinSpace]"]~Prepend~"("~Append~")"]


\[ThinSpace] can be replaced alternatively by \[VeryThinSpace] or \[MediumSpace], depending on how much space you want. These can be entered using Esc+Space+Space+Esc, Esc+Space+Esc, or Esc+Space+Space+Space+Esc, respectively.

Then, note that

br[1, 2, 3]
(* (1 2 3) *)


So that

br[1, 2, 3, 4]/br[2, 3, 4, 1] + br[2, 3, 4, 5]/br[1, 2, 3, 4]
% // TeXForm


returns

Brief (and perhaps incomplete) explanation

When expression are displayed as outputs in Mathematica, they are formatted using Boxes. RowBox in particular accepts a list of expressions for us in displaying them in a row. To convert to the display style, you apply DisplayForm.

To have br display as this output, we use Format. In the call to Format, we have used the pattern br[a__], so anything matching this form will be formatted according to the right-hand-side.

As for the rest, it's all for the purpose of getting the correct list of objects to display in a row. What we want is a list of the inputs of br separated by spaces (of some sort) and bracketed by parentheses. Riffle inserts its second argument in between the elements of the list in its first argument, so

Riffle[ToString /@ {1, 2, 3}, " "]
(* {"1", " ", "2", " ", "3"} *)


where ToString /@ {1, 2, 3} turns all the elements in the list to strings. We then need to Append and Prepend "(" and ")", respectively, to the list, and we have done this using Infix notation. I could just as easily done something like

Join[{"("}, Riffle[ToString /@ {a}, " "], {")"}]


You can use TeXDelimited function from my TeXUtilities package, that was created precisely for this kind of tasks.

Import["https://raw.githubusercontent.com/jkuczm/MathematicaTeXUtilities/master/NoInstall.m"]

ClearAll[br]
Format[br[x__], TeXForm] :=
TeXDelimited["(", x, ")", {"BodySeparator" -> "~", "DelimSeparator" -> ""}]

br[1, 2, 3, 4]/br[2, 3, 4, 1] + br[2, 3, 4, 5]/br[1, 2, 3, 4]
(* br[1, 2, 3, 4]/br[2, 3, 4, 1] + br[2, 3, 4, 5]/br[1, 2, 3, 4] *)
% // TeXForm
(* \frac{(1~2~3~4)}{(2~3~4~1)}+\frac{(2~3~4~5)}{(1~2~3~4)} *)


StringReplace[
ToString@TeXForm@#,
Shortest["\\text{br}(" ~~ s : __ ~~ ")"] :>
"(" <> StringReplace[s, "," -> "~"] <> ")"] &@
(br[1, 2, 3, 4] br[2, 3, 4, 1] + br[2, 3, 4, 1]/br[1, 2, 3, 4])


"\frac{(1~2~3~4)}{(2~3~4~1)}+\frac{(2~3~4~1)}{(1~2~3~4)}"

StringReplace[StringDrop[ToString[TeXForm[br[1, 2, 3, 4]]], 9],
"," -> " "]


$\text{(1 2 3 4)}$

• The output is \text{(1,2,3,4)}, which still has the \text{ } wrapper and still contains commas. See the answer by march in the commets to the question for a working solution. – Kagaratsch Sep 30 '15 at 20:58

Another approach is to use RuleDelayed.

br[2, 3, 4, 1] /.
br[w_Integer, x_Integer, y_Integer, z_Integer] :>
"(" <> Riffle[Map[ToString[#] &, {w, y, y, z}], " "] <> ")"


The inner part produces a list of strings

br[2, 3, 4, 1] /.
br[w_Integer, x_Integer, y_Integer, z_Integer] :>
Map[ToString[#] &, {w, y, y, z}]


{"2", "4", "4", "1"}

Then Riffle is used to interleave spaces and StringJoin (i.e., <>) is used to add parenthesis.

br[1, 2, 3, 4]/br[2, 3, 4, 1] + br[2, 3, 4, 5]/br[1, 2, 3, 4] /.
br[w_Integer, x_Integer, y_Integer, z_Integer] :>
"(" <> Riffle[Map[ToString[#] &, {w, y, y, z}], " "] <> ")"


Applying TeXForm to this output produces

\frac{\text{(1 3 3 4)}}{\text{(2 4 4 1)}}+\frac{\text{(2 4 4 5)}}
{\text{(1 3 3 4)}}