Commits

Anonymous committed 3840ae0

don't write same label twice when called inside "align" and similar
environments; thanks Andrey Novoseltsev for prodding me to fix this

  • Participants
  • Parent commits 3cc4e62

Comments (0)

Files changed (1)

 %    \end{macrocode}
 % Don't remove the space before the percent sign above!
 %
+% \LTX environments such as |align| evaluate their arguments twice after
+% doing |\savecounters@|, so if you do |\sage| inside such an environment,
+% it will result in two labels with the same name in the |.sout| file and
+% the user sees a warning when typesetting. So we keep track of the
+% largest label we've seen so that we don't write two labels with the same
+% name.
+%    \begin{macrocode}
+    self.max_inline_counter_seen = -1
+%    \end{macrocode}
 % Open a |.sout.tmp| file and write all our output to that. Then, when
 % we're done, we move that to |.sout|. The ``autogenerated'' line is
 % basically the same as the lines that get put at the top of preparsed
 % nefarious purposes. The function writes a |\newlabel| line with a
 % label made from a counter and the text from running Sage on |s|.
 %
+% When the user does |\sage| inside certain displayed math environments
+% (|align| is the most common culprit) this function will get called
+% twice with exactly the same arguments. We check to see what labels
+% we've seen and immediately bail if we've written this label before.
+%
 % The |labelname| defaults to the the name used by the usual |\sage|
 % inline macro, but this function is also used by the |sagecommandline|
 % environment. It's important to keep the corresponding labels separate,
 % That's a lot of explanation for a very short function:
 %    \begin{macrocode}
   def inline(self, counter, s, labelname=None):
+      if counter == self.max_inline_counter_seen:
+          return
+      else:
+          self.max_inline_counter_seen += 1
       if labelname is None:
           labelname = 'sageinline'
           self.progress('Inline formula {0}'.format(counter))