3

I wonder whether it is possible to translate an output from the computer algebra system GAP automatically into a nice latex form. I have no experience with programming in latex so Im not sure whether such a thing is possible or whether there exists tools to do an immediate translation.

Here are two examples of such GAP outputs:

[ [ 'x{}'_op_'x{0, 1, 2}' ], [ 'x{2}'_op_'x{0, 1}', 'x{1}'_op_'x{0, 2}', 'x{0}'_op_'x{1, 2}' ], 
  [ 'x{1, 2}'_op_'x{0}', 'x{0, 2}'_op_'x{1}', 'x{0, 1}'_op_'x{2}' ], [ 'x{0, 1, 2}'_op_'x{}' ] ]

This is a list with several entries, the intended latex output should look as follows:

$( \emptyset , \{ 0,1,2 \} ) \rightarrow ( \{2 \} , \{0,1 \} ) \oplus ( \{1 \} , \{0,2 \} ) \oplus ( \{ 0 \} , \{1,2 \} ) \rightarrow  ( \{1,2 \} , \{ 0 \} ) \oplus ( \{ 0,2 \} , \{ 1 \} ) \oplus ( \{ 0,1 \} , \{ 2 \} ) \rightarrow ( \{ 0,1,2 \} , \emptyset )$

The rendered formula the above given code renders to.

Thus the GAP-output consists of several list of the form (example) [ 'x{1, 2}'op'x{0}', 'x{0, 2}'op'x{1}', 'x{0, 1}'op'x{2}' ], which should correspond in latex to $( {1,2 } , { 0 } ) \oplus ( { 0,2 } , { 1 } ) \oplus ( { 0,1 } , { 2 } )$ and the lists are connected in latex by $\rightarrow $.

GAP-output (2. example):

[ [ 'x{}'_op_'x{0, 1, 2}' ], [ 'x{}'_op_'x{0, 1}', 'x{1}'_op_'x{0, 1, 2}', 'x{0}'_op_'x{1, 2}' ], 
  [ 'x{1, 2}'_op_'x{0}', 'x{0}'_op_'x{1}', 'x{0, 1}'_op_'x{1, 2}' ], [ 'x{0, 1, 2}'_op_'x{}' ] ] 

Intended latex form:

$( \emptyset , \{ 0,1,2 \} ) \rightarrow ( \emptyset , \{0,1 \}) \oplus ( \{1\} , \{ 0,1,2 \} ) \oplus ( \{0\} , \{1,2 \}) \rightarrow ( \{1,2 \} , \{0 \} ) \oplus ( \{ 0 \} , \{1 \} ) \oplus ( \{0,1\} , \{1,2 \} ) \rightarrow ( \{0,1,2 \} , \emptyset )$

The rendered formula the above given code renders to.

2
2
+50

It seems like we need to do the transformation by ourselves. Theoretically it is possible to do the transformation in pure latex. But it is a lot easier to write in other programming languages. I wrote a (really hacky) python script that does the job:

gap_input =  """
[ [ 'x{}'_op_'x{0, 1, 2}' ], [ 'x{2}'_op_'x{0, 1}', 'x{1}'_op_'x{0, 2}', 'x{0}'_op_'x{1, 2}' ], 
  [ 'x{1, 2}'_op_'x{0}', 'x{0, 2}'_op_'x{1}', 'x{0, 1}'_op_'x{2}' ], [ 'x{0, 1, 2}'_op_'x{}' ] ]
"""

def listify(s):
    return s.replace('_op_', ', ').replace("x{", "[").replace("}", "]")

def transform_gap_to_tex(gap_input):
    gap_input = eval(gap_input.replace("'_op_'", '_op_'))
    gap_input = map(lambda x: map(lambda y: eval("[%s]" % listify(y)), x), gap_input)
    return "$%s$" % " \\rightarrow ".join(map(lambda x: 
                        "\\oplus".join(map(lambda y: "( %s )" % ", ".join(
                            map(lambda z: "\\{ %s \\}" % str(z)[1:-1], y)
                        ), x)), gap_input)).replace("\\{  \\}", "\\emptyset")

print(transform_gap_to_tex(gap_input))

There are many ways to use something like this in an automated way. There's a nice post about this: How can other programming languages and tools be used to create content for TeX documents?

There's also a package to use python code in latex, but this code in particular will need some extra in order to work this way. ('%' is interpreted as comment in latex)

And if none of the above is a option for you. At least running the script by hand will be less work than doing the transformation by hand.

1
  • Thank you very much! One can test your code here online: sagecell.sagemath.org . I will do some more tests tomorrow and then accept this answer. – Mare Mar 4 '20 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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