Ask Your Question
0

SageTeX-Output: Possible simplification sought

asked 2020-01-05 16:32:29 +0100

geroyx gravatar image

updated 2020-01-05 17:49:56 +0100

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:

image description

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 flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2020-01-05 17:45:42 +0100

dazedANDconfused gravatar image

updated 2020-01-05 17:56:01 +0100

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

image description

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.

edit flag offensive delete link 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!

geroyx gravatar imagegeroyx ( 2020-01-05 17:55:46 +0100 )edit

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

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

Seen: 344 times

Last updated: Jan 05 '20