'How to remove brackets in a reticulate output in R/exams?

I have the following single-choice exercise RadicalPrimo.Rmd, which is intended to simplify square roots:

```{r data generation, echo = FALSE, results = "hide"}
library("exams")
library("reticulate")

# use_python('/usr/bin/python3', required = TRUE)
use_python('/home/rstudio/.local/share/r-miniconda/envs/r-reticulate/bin/python', required = TRUE)
options(scipen=999)
typ <- match_exams_device()
#--------------------------------------------------------
```

```{python, include=FALSE}
from sympy import *   
init_printing()     ##### Facilita la impresión en un formato legible
```

```{python, eval = TRUE, echo = FALSE,results='asis'}
import sympy
import random
nrand1 = random.randint(2, 200)
nram = nrand1*2
sol = sympy.sqrt(nrand1)
pp = print('$' + sympy.printing.latex(sol) + '$')
```

Question
========

Simplify: 

$$\sqrt{`r py$nrand1`}$$


Answerlist
----------

*   
```{python, eval = TRUE, echo = FALSE,results='asis'}
import sympy
print('$' + sympy.printing.latex(sol) + '$')
```

*   
```{python, eval = TRUE, echo = FALSE,results='asis'}
import sympy
import random 
nrand2 = random.randint(1, 200)
if (nrand2 != nrand1) & (nrand2<nrand1):
  inc2 = sympy.sqrt(nrand2)
  inc2b = sympy.root(nrand2,2)
  inc =random.sample((inc2,inc2b),1)
print('$' + sympy.printing.latex(inc) + '$')
```

*  
```{python, eval = TRUE, echo = FALSE,results='asis'}
import sympy
import random
nrand3 = random.randint(2, 200)
if (nrand2 != nrand1) & (nrand3 != nrand2) & (nrand3<nrand1):
  inc3 = sympy.sqrt(nrand3)
pp3 = print('$' + sympy.printing.latex(inc3) + '$')
```

*  
```{python, eval = TRUE, echo = FALSE,results='asis'}
import sympy
import random
nrand4 = random.randint(2, 200)
if (nrand2 != nrand1) & (nrand3 != nrand2) & (nrand4 != nrand3) & (nrand4<nrand1):
  inc4 = sympy.sqrt(nrand4)
pp4 = print('$' + sympy.printing.latex(inc4) + '$')
```

Solution
========

```{python, eval = TRUE, echo = FALSE,results='asis'}
import sympy
print('$' + sympy.printing.latex(sol) + '$')
``` 

Meta-information
================
exname:RadicalPrimo(single-choice)
extype:schoice
exsolution: 1000
exshuffle: TRUE

However, some answers in the choice list have inelegant square brackets:

enter image description here

I have applied various methods to remove brackets in Python output, but to no avail. How can I permanently remove those brackets?



Solution 1:[1]

In your example inc, the output of random.sample((inc2,inc2b),1) is a list in Python. Hence, when printed (both within Python or as LaTeX) the brackets are added:

>>> type(inc)
## <class 'list'>
>>> inc
## [sqrt(67)]
>>> sympy.printing.latex(inc)
## '\\left[ \\sqrt{67}\\right]'

You can simply extract the first element and print that:

>>> inc = inc[0]
>>> type(inc)
## <class 'sympy.core.power.Pow'>
>>> inc
## sqrt(67)
>>> sympy.printing.latex(inc)
## '\\sqrt{67}'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Achim Zeileis