1

I'm trying to work out how to recursively nest text and math mode. The following example seems to fail to compile, and does not render the \right):

% "text, {$$\int$$}, text" is an arbitrary string provided by software I
% do not control, 
$$ \operatorname{foo}\left( \text{text, {$$\int$$}, text} \right)$$

Is there a simple way to make this work?

Eric
  • 669
  • 1
    You cannot use $$...$$ within \text. I guess you want simply \text{text, $\int$, text}. – campa Jul 21 '20 at 07:43
  • Is the a text environment I can enter from math mode within which $$ can be used? – Eric Jul 21 '20 at 07:45
  • 1
    Sorry, what but should that exactly do? $$ (which shouldn't be really used in LaTeX anyway) enters display math mode, so you are trying to enter display math within display math. What are you trying to achieve? – campa Jul 21 '20 at 07:47
  • My motivation is computational - in python have a function sympy.latex(x) which must return math-mode latex, but for some x the only thing available to me is x._repr_latex_() which returns text-mode latex. I'm looking for the best way of converting the latter to the former, while breaking as few constructs as possible. This question addresses failings the naive approach of math_mode = "\\text{" + text_mode + "}" – Eric Jul 21 '20 at 07:52
  • just use single dollars, $$ is not latex syntax and makes display math which is not what you want here. – David Carlisle Jul 21 '20 at 08:32
  • 1
    it's python: you must be able to regex replace $$ by $ to fix the returned string before using it. – David Carlisle Jul 21 '20 at 08:39
  • @DavidCarlisle: Sure, but I was really trying to avoid having to write a latex parser to deal with cases like \$$, \\$$, etc. – Eric Jul 21 '20 at 13:20
  • @Eric well, currently it works in no cases. If you replace $$ by $ it will work in all but the 0.00001% of cases where you have a \$ immediately before a $, so on balance, a win. – David Carlisle Jul 21 '20 at 14:09

1 Answers1

1

The software you're using is flawed and there's essentially no safe method for making such generated code to work.

The problem is that \text eventually makes an \hbox in which the code is typeset using restricted horizontal mode where $$ has a peculiar behavior. Since display math makes no sense, TeX simply considers $$ as an empty math formula, when in restricted horizontal mode. So your \int appears outside math mode and an error ensues.

TeX now tries to enter math mode before rescanning \int, then finds the closing $ which ends math mode. It finds another one, which starts again math mode and now it finds a misplaced }. Another error.

Fix the software producing that code, which is bad LaTeX under many other respects. To begin with, $$ should never be used in LaTeX.

egreg
  • 1,121,712
  • "To begin with, $$ should never be used in LaTeX." - it would be helpful to know what should be used in its place. – Eric Jul 21 '20 at 13:20
  • 1
    @Eric \[ see https://tex.stackexchange.com/a/69854/1090 But you do not want either here. $$\int$$ is a full width display, it has vertical space a math display the full width of the page then more vertical space. It makes no sense to place it between commas and parenthesis as in your example. You want inline math here not just avoiding the error message. – David Carlisle Jul 21 '20 at 14:08
  • "Fix the software producing that code, which is bad LaTeX under many other respects" - I do not have the power to fix the universe - the code returning text $$\int$$ text is in this example coming from a hypothetical third-party package, via a very lax interface specification I also do not control. – Eric Aug 11 '20 at 17:28