0

first of all, I am sorry if this has been already answered, I can't find any anwser !

When I try to compile this MWE :

% % % xelatex document.tex
% % % pythontex document.tex
% % % xelatex document.tex



\documentclass[12pt, twocolumn]{article}

\usepackage{xcolor}

\usepackage{xlop}                       % Pour les calculs posés
\opset{decimalsepsymbol={,},carrystyle=\scriptsize\bfseries,
carrysub,lastcarry,offsetcarry=-0.4,
displayintermediary=all,displayshiftintermediary=all}




\usepackage{multicol, pgffor, multido}
\usepackage{amsmath, amssymb}
\usepackage[pyfuture=none, gobble=auto]{pythontex}



\begin{document}

\begin{pycode}
import random
#
a=random.randint(50,1000)
b=random.randint(3,20)
q=a//b
r=a%b
def next():
   a=random.randint(50,1000)
   b=random.randint(3,20)
   q=a//b
   r=a%b
   return a,b,q,r
\end{pycode}

\pyc{a,b,q,r=next()}

$\dfrac{\py{a}}{\py{b}} = \textcolor{red}{\py{q}}+\dfrac{\textcolor{blue}{\py{r}}}{\py{b}}$

\opidiv[voperation=top, dividendbridge,resultstyle=\color{red}, remainderstyle=\color{blue}]{\py{a}}{\py{b}}


\label{LastPage}
\end{document}

I get errors :

Package PythonTeX Warning: Missing autoprint content on input line 59.

! Improper alphabetic constant.
<to be read again> 
                   \py 
l.61 ...mainderstyle=\color{blue}]{\py{a}}{\py{b}}

but if I comment out the line with \opidiv...., there is no problem.

How to make it run without errors ?

Thank you.

2 Answers 2

1

A similar situation is described in the pythontex manual in Section 8 Troubleshooting on page 57-59. There the example of siunitx is used, which needs to expand its arguments (i.e., inspect the final value returned by any macros inside of the argument), and \py is not expandable. For \opidiv the issue is the same as for \SI described in the manual.

The solution is also the same: construct the full macro call from Python, which means the values of the Python variables are inserted in the macro string in expanded form before xlop 'sees' them.

Here a version that uses the global a and b variables. Analogous to the manual example you can also construct a version that takes arguments from within the LaTeX code.

\documentclass[12pt, twocolumn]{article}

\usepackage{xcolor}

\usepackage{xlop}                       % Pour les calculs posés
\opset{decimalsepsymbol={,},carrystyle=\scriptsize\bfseries,
carrysub,lastcarry,offsetcarry=-0.4,
displayintermediary=all,displayshiftintermediary=all}

\usepackage{multicol, pgffor, multido}
\usepackage{amsmath, amssymb}
\usepackage[pyfuture=none, gobble=auto]{pythontex}

\begin{document}

\begin{pycode}
import random
#
a=random.randint(50,1000)
b=random.randint(3,20)
q=a//b
r=a%b
def next():
   a=random.randint(50,1000)
   b=random.randint(3,20)
   q=a//b
   r=a%b
   return a,b,q,r

def xlopdif():
   return r'\opidiv[voperation=top, dividendbridge,resultstyle=\color{red}, remainderstyle=\color{blue}]{' + str(a) + '}{' + str(b) +'}'
\end{pycode}

\pyc{a,b,q,r=next()}

\section{dfrac version}
$\dfrac{\py{a}}{\py{b}} = \textcolor{red}{\py{q}}+\dfrac{\textcolor{blue}{\py{r}}}{\py{b}}$

\section{xlop version}
\py{xlopdif()}

\label{LastPage}
\end{document}

Result:

enter image description here

2
  • Thank you so much +++
    – silver
    Jan 22, 2023 at 15:45
  • @silver In that case you should upvote and accept the answer.
    – user202729
    Jan 22, 2023 at 16:44
0

A less verbose alternative is to use pys command (or pysub environment).

% % % xelatex document.tex
% % % pythontex document.tex
% % % xelatex document.tex



\documentclass[12pt, twocolumn]{article}

\usepackage{xcolor}

\usepackage{xlop}                       % Pour les calculs posés
\opset{decimalsepsymbol={,},carrystyle=\scriptsize\bfseries,
carrysub,lastcarry,offsetcarry=-0.4,
displayintermediary=all,displayshiftintermediary=all}




\usepackage{multicol, pgffor, multido}
\usepackage{amsmath, amssymb}
\usepackage[pyfuture=none, gobble=auto]{pythontex}



\begin{document}

\begin{pycode}
import random
#
a=random.randint(50,1000)
b=random.randint(3,20)
q=a//b
r=a%b
def next():
   a=random.randint(50,1000)
   b=random.randint(3,20)
   q=a//b
   r=a%b
   return a,b,q,r
\end{pycode}

\pyc{a,b,q,r=next()}

$\dfrac{\py{a}}{\py{b}} = \textcolor{red}{\py{q}}+\dfrac{\textcolor{blue}{\py{r}}}{\py{b}}$

\pys{\opidiv[voperation=top, dividendbridge,resultstyle=\color{red}, remainderstyle=\color{blue}]{!{a}}{!{b}}}


\label{LastPage}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .