Ask Your Question

Sage code appears in different places on different compiles

asked 2015-11-25 01:21:12 +0200

Jason021 gravatar image


I write things like exams for math classes, and like to use sagetex to create random problems and the appropriate solutions (for multiple choice exams). Everything with sagetex works fine in terms of the installation (using it on a chromebook, and I have sage fully installed and synced up with the local latex installation). I compile the latex code by doing a pdflatex, then sagetex, then pdflatex again.

The problem I seem to have is that the sagetex code locations seem to appear in the wrong places, and it moves around each time I compile the same code. Thus I will get equations that are suppose to be the questions placed in answers and vice versa, even across different questions.

I've been trying to figure out what the issue is, but haven't had much luck. I originally used code to randomize locations of things, and set the seed to update slowly enough that it wouldn't change over while it was compiling to make sure stuff lined up (and most of it did). Then to try a failsafe I fixed the seed to a static value so that it always locates things to the same places every time, and the problem still persists.

The odd thing is it persists in a totally non-uniform way. Sometimes I will compile the code and everything is where it should be. Sometimes everything after the 10th sage entry is jumbled. Sometimes just the last couple.

When it fails, it always completely fails after a certain point, and what it 'looks' like is the case is that it skips some of the sage input and moves ahead to future input (so that the last few sage inputs are left as ??? after the second pdflatex run). So it works fine up to a point, then jumbles things by skipping seemingly random sagetex code.

So. Any idea what gives? My sample code is sort of involved, so I can post it if you guys want but it involves a long latex file and a rather significant cls file, so I didn't want to post it all here (I can maybe post a link to the files if it helps).

Thanks for the help, I've been trying to figure this quirk out for months with no luck.

PS A MWE doesn't really work because this seems to be a problem that crops up more reliably the longer the example is. Here are the files that seem to (fairly reliably) recreate the problem...


edit retag flag offensive close merge delete


Have you tried running your code on SageMathCloud at Does the same phenomenon occur there too?

slelievre gravatar imageslelievre ( 2015-11-25 09:22:52 +0200 )edit

This occurred to me a while back and I looked into it (admittedly briefly) as I am not sure how to load a cls file into sagemathcloud, does it support cls files? If so I'll go back and see if I can figure it out. I hadn't thought about this recently though, and revisiting it seems like a pretty good idea, thanks!

Jason021 gravatar imageJason021 ( 2015-11-25 17:12:02 +0200 )edit

There should be no problem using latex class files (.cls) on SageMathCloud.

slelievre gravatar imageslelievre ( 2015-11-25 17:32:32 +0200 )edit

I uploaded it to sagemathcloud and it does still have the same issue. I have shared it here:

Jason021 gravatar imageJason021 ( 2015-11-25 19:03:40 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2015-11-30 08:33:34 +0200

slelievre gravatar image

From a quick glance, the latex class you are using seems to be full of code designed to display things in a random order. So, it's maybe not so surprising to see exactly that happening when using it.

edit flag offensive delete link more


Well, you're sort of right. The doc class is designed to store the content and then reorder it randomly in blocks, then redisplay it. The problem is that the sagetex doesn't move with the blocks as one would like because of that middle step of writing the location of the sage lines to an external file, then having stuff move around. Even more confusing is that this is still a problem with fixed randomizing seed (so it "randomly" puts everything in the same place) A possible solution to my problem might be if there is a way to write a latex command to store the result of the sage code without having to recall the sage code each time that command is called. i.e. if I do \newcommand{\foo}{\sage{function}} it will call \sage{function} each time, not the content of \sage{function} directly.

Jason021 gravatar imageJason021 ( 2015-12-07 23:22:35 +0200 )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


Asked: 2015-11-25 01:10:34 +0200

Seen: 807 times

Last updated: Nov 30 '15