I would like to know how to put punctuation after a $$ without line break and without putting this punctuation inside.

For example : We have $$1+1=2$$, so $$1+1\neq 3$$. returns

Example 1

while the result I would like to get is (no line break before the punctuation)

Example 2

made with the command We have $$1+1=2,$$ so $$1+1\neq 3.$$.

But I don't want to place the punctuation characters inside the math environments (it looks wrong to me) and so I would like to have the second result but with the command We have $$1+1=2$$, so $$1+1\neq 3$$..

Have you got any solution ?

Thanks for your help !


  • 11
    Unrelated but don't use $$ in LaTeX. Displaymath is done with \[ and \]. – Ulrike Fischer Mar 21 at 10:52
  • 3
    You should not use $$; and yes, the punctuation goes inside. – egreg Mar 21 at 10:53
  • Why it's bad to use $$ ? And why the punctuation goes inside, it's not a math , but a regular text , that I want (for example if I change my fonts)... @egreg @Ulrike Fischer – Thomas Mar 21 at 10:55
  • 9
    See Why is \[ … \] preferable to $$?. As for the punctuation, it needs to go inside, because there is no “look ahead” for it. You can use \text{,}, but this would mean you're using incompatible text and math fonts. – egreg Mar 21 at 10:59
  • 3
    See tex.stackexchange.com/questions/503/why-is-preferable-to. And you can use \mbox{,} or (with amsmath) \text{,} to switch to text mode inside math if you want. – Ulrike Fischer Mar 21 at 10:59

As Ulrike and Enrico have already pointed out in comments, (a) the punctuation characters do go inside the display math group and (b) it's much better to write \[ ... \] rather than $$ ... $$ in a LaTeX document.

If, however, you can't break either habit, and if you're able to use LuaLaTeX, the following solution may be what you're looking for. The solution, as shown below, currently works for only two punctuation characters, viz., , (comma) and . (period, aka "full stop"); however, it's straightforward to extend it to handle additional punctuation characters, such as ; and :. Furthermore, do observe that the code (a) automatically inserts a thinspace before the punctuation characters, (b) places the punctuation character in text mode -- just in case the text-mode and math-mode punctuation characters aren't the same -- and (c) allows whitespace (but not a line break) between the closing $$ character pair and the punctuation character.

enter image description here

% !TEX TS-program = lualatex
function switch_punctuation ( s )
   return ( s:gsub ( "%$%$%s-([%.%,])" , "\\,\\mbox{%1}$$" ) )
    "process_input_buffer", switch_punctuation , "switchpunctuation" )}}

We have $$1+1=2$$, so $$1+1\neq 3$$ .
| improve this answer | |

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.