# SageTeX-Output: Possible simplification sought

I created a minimal example for SageTeX:

x = 420       # Denominator
MyRange = 222

M = []
for n in range (1,MyRange):
if x == denominator(n/x):
M.append('$' + latex(n/x).replace(' ','') + '$')
else:
M.append('$' + '\\frac{' + "{}".format(n) + '}{' + "{}".format(x) + '}=' + latex(n/x).replace(' ','') + '$')

MyOut = ', '.join(M)
print(MyOut)


This gives:

It works! Just for interest, I wonder: does the 2nd else-output
M.append('$' + '\\frac{' + "{}".format(n) + '}{' + "{}".format(x) + '}=' + latex(n/x).replace(' ','') + '$')
really have to be so complicated or is there an easier way?

€dit: My SageTeX-MWE is:

\documentclass{article}
\usepackage{amsmath, amssymb, amsfonts}
\usepackage{sagetex}

\begin{document}
\section{In}
\begin{sageblock}
x = 420       # Denominator
MyRange = 222

M = []
for n in range (1,MyRange):
if x == denominator(n/x):
M.append('$' + latex(n/x).replace(' ','') + '$')
else:
M.append('$' + '\\frac{' + "{}".format(n) + '}{' + "{}".format(x) + '}=' + latex(n/x).replace(' ','') + '$')

MyOut = ', '.join(M)
#print MyOut
\end{sageblock}

\section{Out}
\baselineskip16pt \sagestr{MyOut}
\end{document}

edit retag close merge delete

Sort by » oldest newest most voted

I'm a little confused by your question. First, you haven't created a SageTeX example. A SageTeX example is a LaTeX file that relies on the SageTeX package to create output. Second, your output gives LaTeX output but not for your code since your denominator is 16 and not 420. Here is a SageTeX example for your problem

\documentclass{article}
\usepackage{sagetex}
\begin{document}
\begin{sagesilent}
x = 16       # Denominator
MyRange = 16
output = r""
for n in range (1,MyRange):
if  n<MyRange-1:
if gcd(n,x)==1:
output += r"\frac{%s}{%s}, "%(n,x)
else:
output += r"\frac{%s}{%s}=\frac{%s}{%s}, "%(n,x,numerator(n/x),denominator(n/x))
else:
output += r"\frac{%s}{%s}"%(n,x)
\end{sagesilent}
\noindent $\sagestr{output}$
\end{document}


When run using LaTeX this gives the picture below

The approach I used was to see if the numerator and denominator were coprime, if gcd(n,x)==1, in which case output the fraction as is, otherwise typeset fraction and then what it simplifies to, using numerator() and denominator. Finally, to account for the commas and final fraction with no comma after it, I printed out commas until the last term, which I assume was 1 less than MyRange. This seems easier to me because I've avoided the lengthy append you've created when the fraction simplifies.

more

@dazedANDconfused I am sorry, I played with the values, but only in tex-file for which a added the code now. But your way is better! Thx!

( 2020-01-05 10:55:46 -0600 )edit