4

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 !

Thomas

Mico
  • 506,678
Thomas
  • 137
  • 11
    Unrelated but don't use $$ in LaTeX. Displaymath is done with \[ and \]. – Ulrike Fischer Mar 21 '20 at 10:52
  • 3
    You should not use $$; and yes, the punctuation goes inside. – egreg Mar 21 '20 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 '20 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 '20 at 10:59
  • 3
    See https://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 '20 at 10:59
  • The gods of TeX designed it for punctuation to go in the formula (Knuth and Spivak, etc.). I checked using a simple comma after a formula vs \text{,} and zoomed in a million fold...no difference. I use $$...$$ for latex. It can apparently mess up spacing if you use equations flush left and perhaps cause other esoteric issues. What I wouldn't do is to write a display equation in the text such as $$E=mc^2$$. Rather, I start a new line for display equation whether I am being naughty and using double dollar sign or not. This question brings up the issue with TeX, you can waste away hours getting – Robert McLean MD PhD Mar 21 '20 at 23:49
  • @RobertMcLeanMDPhD - Please provide evidence for your claim that "The gods of TeX designed it for punctuation to go in the formula (Knuth and Spivak, etc.)". My impression is quite different: Knuth took a careful look at specimens of fine math typographyand distilled what he found into the design of displayed equations produced by TeX. It would also be useful if you provided some background information regarding your second claim, "I checked using a simple comma after a formula vs \text{,} and zoomed in a million fold...no difference." E.g., did you use the default text and math fonts? – Mico Mar 22 '20 at 09:43

1 Answers1

14

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
\documentclass{article}
\usepackage{luacode}
\begin{luacode}
function switch_punctuation ( s )
   return ( s:gsub ( "%$%$%s-([%.%,])" , "\\,\\mbox{%1}$$" ) )
end
\end{luacode}
\AtBeginDocument{\directlua{luatexbase.add_to_callback(
    "process_input_buffer", switch_punctuation , "switchpunctuation" )}}

\begin{document}
We have $$1+1=2$$, so $$1+1\neq 3$$ .
\end{document}
Mico
  • 506,678