1

Would some body please help me in getting the plots, I have tried many things, like creating function('f') and then passing it like Q1 = question(1, f(x+1)) but the thing that only works is: output += r"\sageplot{plot(x+1, figsize = 2)} \\\\" which I don't want:

Here is my minimal code:

\documentclass{article}
 \usepackage{sagetex}
 \usepackage{multicol}
 \usepackage[margin=0.5in]{geometry}
 \usepackage{amsmath}
 \newcommand\answerbox{\fbox{\rule{1in}{0pt}\rule[-0.5ex]{0pt}{4ex}}}

  \begin{document}


  \begin{sagesilent}
   def question(n, f):
     output = r""
     output += r"\bgroup"
     output +=r"\noindent\textbf{Question %s} \\\\" %(n)
     output +=r"\sageplot{plot(f, xmin =-2, xmax=2, ymin=-2, ymax=3, figsize = 2)} \\\\"
     output += r"\textbf{Equation:}"
     output += r"\answerbox \\\\"
     output += r"\egroup"
     return output
  \end{sagesilent}

  \begin{sagesilent}
   Q1 = question(1, 1+x)
   Q2 = question(2, 1+x^2)

  \end{sagesilent}
 \setlength{\columnsep}{2.0in} %Column separation.
 \begin{multicols*}{2}
 \sagestr{Q1}
 \sagestr{Q2}
 \end{multicols*}

 \end{document}
Sohail
  • 115
  • 3

2 Answers2

2

One way would be to define a LaTeX environment "question".

\documentclass{article}

\usepackage[margin=0.5in]{geometry}
\usepackage{multicol}
\usepackage{amsmath}
\usepackage{url}
\usepackage{sagetex}

\title{What function is this a plot of?}
\author{TeX Stack Exchange question 535740}
\date{\url{https://tex.stackexchange.com/q/535740}}

\newenvironment{question}[8]{%
%% usage: \question{number}{function}{xmin}{xmax}{ymin}{ymax}{aspectratio}{figsize}
\bigskip\noindent
\textbf{Question #1}\par
\sageplot{plot(lambda x: #2, (#3, #4), ymin=#5, ymax=#6, aspect_ratio=#7, figsize=#8)}\par
\textbf{Equation: }\answerbox\par\medskip%
}

\newcommand\answerbox{\fbox{\rule{1.5in}{0pt}\rule[-0.5ex]{0pt}{4ex}}}

\begin{document}%
\maketitle

\setlength{\columnsep}{2.0in} %Column separation.
\begin{multicols*}{2}

\question{1}{1 + x}{-2}{2}{-1}{3}{1}{4}

\question{2}{1 + x^2}{-2}{2}{0}{5}{0.8}{4}

\end{multicols*}

\end{document}
2

It looks like you've followed the Cocalc answer to this question. For this to work your variables, n and f, must be substituted into the strings. You did this, for example, in the line output +=r"\noindent\textbf{Question %s} \\\\" %(n). Having Question n will not work. For that same reason you can't put plot(f on the next line. You need plot(%s and substitute f in. I've modified your code below:

\documentclass{article}
\usepackage{sagetex}
\usepackage{multicol}
\usepackage[margin=0.5in]{geometry}
\usepackage{amsmath}
\newcommand\answerbox{\fbox{\rule{1in}{0pt}\rule[-0.5ex]{0pt}{4ex}}}
\begin{document}
\begin{sagesilent}
    def question(n, f):
        output = r""
        output += r"\bgroup"
        output +=r"\noindent\textbf{Question %s} \\\\"%(n)
        output +=r"\sageplot{plot(%s, xmin =-2, xmax=2, ymin=-2, ymax=3, figsize = 2)} \\\\"%(f)
        output += r"\textbf{Equation:}"
        output += r"\answerbox \\\\"
        output += r"\egroup"
        return output

    Q1 = question(1, 1+x)
    Q2 = question(2, 1+x^2)
\end{sagesilent}
\setlength{\columnsep}{2.0in} %Column separation.
\begin{multicols*}{2}
\sagestr{Q1}
\sagestr{Q2}
\end{multicols*}
\end{document}

Which will give you this output: enter image description here

DJP
  • 12,451
  • yes, I am the same person that asked the referenced question. I spent almost 10 hrs trying different options but never thought of this. Thanks for your time.. Have a great day!!! – Sohail Mar 29 '20 at 18:46
  • No problem. Don't be afraid to ask for help: 10 hours is too long to struggle with a problem when there are plenty of people who can help. – DJP Mar 29 '20 at 23:00