Ask Your Question

# 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

## 1 Answer

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

## Comments

@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 17:55:46 +0200 )edit

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2020-01-05 16:32:29 +0200

Seen: 167 times

Last updated: Jan 05 '20