0

I have generated an exercise problem for a function table as

\documentclass{article}
 \usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}
 \usepackage{fancyhdr}
 \usepackage{multicol}
 \usepackage{amsmath} 
 \lhead{Name:}
 \chead{Function Table}
 \rhead{Date:}
 \lfoot{}
 %\cfoot{\thepage}
 \rfoot{}
 \pagestyle{fancy}
 \newcommand{\question}[7]{\noindent \textbf{Question #1:} $f(x)$ = #2 
 \\[\baselineskip] 
 \begin{tabular}{|c|c|c|c|c|c|}
 \hline
 $x$ & #3 & #4 & #5 & #6 & #7 \\
 \hline
 $f(x)$ & & & & & \\
 \hline
 \end{tabular} \\[\baselineskip]}
 \begin{document}
 \noindent
 \question{1}{$x+1$}{-3}{-2}{0}{1}{2}
 \end{document}

Now I want to create a solution on the next page but want to fill the missing values of f(x) automatically for each question, is it possible? perhaps using sagetex?

5
  • Welcome to TeX.SE. – Mico Jul 15 '19 at 9:35
  • 2
    Are you willing to entertain a LuaLaTeX-based solution, or must your document be compilable under pdfLaTeX? – Mico Jul 15 '19 at 9:40
  • @Mico Thanks for responding and for the welcome! I have never heard of lua before, but after your comment I searched it a bit and found it requires understanding of lua. I really hope somebody will come up with 'pure' solution else I would have no option but to see lua and your solution. – Sohail Jul 15 '19 at 10:41
  • The creating of solution easily can do with ctan.org/pkg/exsheets (or ctan.org/pkg/xsim). The function parsing maybe can do with pgfmath (ctan.org/pkg/pgf) - I'm using it to create exams with plotting functions. – uzsolt Jul 15 '19 at 11:17
  • You could use the fp package, defining a function f(x) as in the answer for this question. – jorgepz Jul 15 '19 at 11:56
1

Here is an approach using the sagetex package, found here. I've put all the output on one page for illustrative purposes. Adding \newpage in the appropriate places will give the effect you've asked for:

\documentclass{article}
\usepackage{sagetex}
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}
\usepackage{fancyhdr}
\usepackage{multicol}
\usepackage{amsmath} 
\lhead{Name:}
\chead{Function Table}
\rhead{Date:}
\lfoot{}
%\cfoot{\thepage}
\rfoot{}
\pagestyle{fancy}
\begin{document}
\begin{sagesilent}
def Qtable(n,f,v1,v2,v3,v4,v5):
  f(x) = f
  output = r""
  output += r"\noindent\textbf{Question $%s$}\\\\"%(n)
  output += r"\begin{tabular}{|c|c|c|c|c|c|}"
  output += r"\hline"
  output += r"$x$ & $%s$ & $%s$ & $%s$ & $%s$ & $%s$\\"%(v1,v2,v3,v4,v5)
  output += r"\hline"
  output += r"$f(x)=%s$ &  &  &  & & \\"%(f(x))
  output += r"\hline"
  output += r"\end{tabular}"

  return output

def Atable(n,f,v1,v2,v3,v4,v5):
  f(x) = f
  output = r""
  output += r"\textbf{Question $%s$ solution}\\\\"%(n)
  output += r"\begin{tabular}{|c|c|c|c|c|c|}"
  output += r"\hline"
  output += r"$x$ & $%s$ & $%s$ & $%s$ & $%s$ & $%s$\\"%(v1,v2,v3,v4,v5)
  output += r"\hline"
  output += r"$f(x)=%s$ & $%s$ & $%s$ & $%s$ & $%s$ & $%s$\\"%(f(x),f(v1),f(v2),f(v3),f(v4),f(v5))
  output += r"\hline"
  output += r"\end{tabular}"

  return output
\end{sagesilent}


\begin{sagesilent}
Q1 = Qtable(1,x+1,-3,-2,0,1,2)
Q1A = Atable(1,x+1,-3,-2,0,1,2)
\end{sagesilent}
\sagestr{Q1}\\\\
\sagestr{Q1A}\\\\

\begin{sagesilent}
Q2 = Qtable(2,x^2-2*x+1,-4,-1,0,3,12)
Q2A = Atable(2,x^2-2*x+1,-4,-1,0,3,12)
\end{sagesilent}
\sagestr{Q2}\\\\
\sagestr{Q2A}
\end{document}

The output running in Cocalc is: enter image description here

Remember, sagetex is part of the LaTeX distribution but it relies on the computer algebra system SAGE which is not. You either need SAGE installed locally on your computer or you use the free Cocalc to get access to SAGE over the internet. The compilation using sagetex is a 3 step process: first LaTeX runs, then SAGE runs, then LaTeX runs again. It helps to think that the first run of LaTeX will create a blank box for the SAGE result, then SAGE calculates results, and the final LaTeX run fills in the box. The program won't run if the SAGE calculation is needed in the LaTeX compilation. To get finesse these problems the LaTeX code is created by SAGE. A Python function Qtable (for question table) will create the LaTeX code for the blank table while Atable (for answer table) will create the table filled in correctly. The third run through the process, running LaTeX, is now compiling a document that is longer as it contains LaTeX code that SAGE created.

The easiest way to get started with SAGE and sagetex is by signing up for a free Cocalc account

3
  • Thank you very much!!! – Sohail Jul 15 '19 at 22:49
  • I would be grateful if you could help me in one more problem, I am having. I have trouble displaying some functions aesthetically nice. To overcome this, I did output += r"$f(x)=%s$ & & & & & \\"%latex((f(x))) this solves one problem of like representing 2*x as 2x but for functions like $(2x^3+3)/(x+1)$, the display does not appear good. I even modified the output to output += r"$f(x)=\displaystyle%s$ & & & & & \\"%latex((f(x))), but this does not work either. Any suggestions? – Sohail Jul 16 '19 at 1:10
  • Your comment: "the display does not appear good" doesn't make clear what the issue for you is. I think you're referring to the fact that the contents of the second line are cramped in the cell. This can be handled with modifications along the lines of output += r"\rule{0pt}{18pt}$f(x)= \displaystyle %s$ & $%s$ & $%s$ & $%s$ & $%s$ & $%s$\\[8pt]"%(latex(f(x)),f(v1),f(v2),f(v3),f(v4),f(v5)) where \rule{0pt}{18pt} will adjust space between top of box and entry while \[8pt] will add more space at the bottom of the entry. Adjust 18pt and 8pt to numbers which you think look best. – DJP Jul 16 '19 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.