4

How can I put straight quotes inside a math equation?

I would like to use straight quotes to surround a string-like piece of data that is displayed using \mathtt. Single or double quotes are both acceptable to me.

This code

\documentclass{article}
\begin{document}

$fn(\mathtt{"a"})$

\end{document}

will generate curly quotes.

7
  • What does your preamble looks like? What packages do you use, that a relevant to the question? In other words: could you provide a minimal example?
    – Clément
    Jan 25, 2016 at 17:57
  • A provided a minimal example, but I wonder if it really makes a difference in this case. Using codecogs with just the equation I get the same result.
    – gioele
    Jan 25, 2016 at 18:16
  • 2
    I would use \texttt instead of \mathtt, i.e., I'd write $fn(\texttt{"a"})$.
    – Mico
    Jan 25, 2016 at 18:16
  • 1
    Since @egreg has already mentioned the possibility of using \texttt toward the end of his answer, I think I should hold off on posting a separate answer that mentions \texttt. As far I can tell, \texttt and \mathtt are equally correct/idiomatic; it's just that \texttt provides an easier method for achieving your objective.
    – Mico
    Jan 26, 2016 at 8:30
  • 1
    @Clément definitely Babel will interfere if an attempt is made to make " math active at the same time it is a Babel shorthand (case of german language). However, changing the mathcode of " to something else than "8000 would not interfere.
    – user4686
    Jan 26, 2016 at 13:01

2 Answers 2

4

If you use " in mathematical formulas, it makes sense to define it so that it always produces straight quotes:

\documentclass{article}
\usepackage{amsmath}
\usepackage{textcomp}

\DeclareMathSymbol{\mathdblquotechar}{\mathalpha}{letters}{`"}

\newcommand{\mathdblquote}{\mathtt{\mathdblquotechar}}
\begingroup\lccode`~=`"\lowercase{\endgroup
  \let~\mathdblquote
}
\AtBeginDocument{\mathcode`"="8000 }

\begin{document}

$\mathit{fn}(\mathtt{"a"})$

$\mathit{fn}(\texttt{"a"})$

And $"a"$

\end{document}

Of course, \texttt{"a"} would be easier.

enter image description here

For single quotes, the simplest strategy is to define a new symbol font and use the TS1 encoded font. However I'd use the backquote in order not to override the usage of ' in math.

\documentclass{article}
\usepackage{amsmath}
%\usepackage{textcomp}

\DeclareSymbolFont{textcomp}{TS1}{\ttdefault}{m}{n}
\DeclareMathSymbol{`}{\mathord}{textcomp}{39}

\DeclareMathSymbol{\mathdblquotechar}{\mathalpha}{letters}{`"}

\newcommand{\mathdblquote}{\mathtt{\mathdblquotechar}}
\begingroup\lccode`~=`"\lowercase{\endgroup
  \let~\mathdblquote
}
\AtBeginDocument{\mathcode`"="8000 }

\begin{document}

$\mathit{fn}(\mathtt{"a"})$ and $\mathit{fn}(\mathtt{`a`})$

$\mathit{fn}(\texttt{"a"})$ and $\mathit{fn}(\texttt{'a'})$

And $"a"$ and $`a`$

\end{document}

enter image description here

Note that \texttt would do the wrong thing.

7
  • I propose you try with \usepackage[german]{babel} ... I deny all responsability to what will happen with your computer...
    – user4686
    Jan 26, 2016 at 12:58
  • @jfbu Happily I'm not using German. ;-)
    – egreg
    Jan 26, 2016 at 13:16
  • as in its Sturm und Drang exaggeration mathastext thought some of its users would, it had to go through quite some complexities to handle such interactions with Babel shorthands one wish to have mathematically active...
    – user4686
    Jan 26, 2016 at 13:19
  • @jfbu In that case, probably directly using \mathdblquote is the simplest choice.
    – egreg
    Jan 26, 2016 at 13:56
  • 1
    @alper I added a solution
    – egreg
    Apr 20, 2022 at 16:04
3

You could declare the quote to be a variable like the chars, but this will have side effects if you use them outside \mathXX-fonts:

\documentclass{article}

\DeclareMathSymbol{"}{\mathalpha}{letters}{`"}
\begin{document}

$"a" \quad \mathtt{"a"}$

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .