4

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.

plauer
  • 438
Mare
  • 291
  • 1
  • 7
  • 1
    The "gap" tag, even though it doesn't have a definition, is typically used to ask about some (usually unwanted) space. So it's not the same thing. I've asked on the chat for suggestions. – barbara beeton Feb 25 '20 at 18:25
  • 1
    Is there anything useful in the manual: https://www.gap-system.org/Manuals/doc/ref/chap27.html#X78024C8087F3E07F – Scott H. Mar 03 '20 at 15:42

1 Answers1

4

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.

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