# How to escape a quotation mark character/glyph?

I have the same problem as in this question, but the answers just circle around how to best use quotation marks. This is fine as long as you actually want a quotation mark. However, I explicitly don't want it.

And there is one legitimate reason: I want to display code. Verbatim. Without any change. So I put that in \texttt, or in a special command I have defined that also just does this.

MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}

% you may add magic here to make it ignore/escape any special characters
\newcommand{\inlinecode}{\texttt}

% disable " making strange characters, it breaks our code quotations
% \shorthandoff{"} % does not fully work (see below)

\title{shorthandoff}
\date{August 2020}

\begin{document}

\maketitle

\section{Introduction}

This is a JSON snippet:
\inlinecode{"example\_string\_mentioned": "yes\_no"} \\
\inlinecode{"okay"} \\
\inlinecode{"yikes"}\\
\inlinecode{"ohno"}\\

\end{document}



This resullts in this:

This is a JSON snippet: ëxample string mentioned": "yes no"
ökay"
"yikes"
öhno"

Obviously this is bad. Obviously I don't want to use typographically correct quotation marks.

• Escape as {"} (caused display errors and showed errors in the console)¹ or as \" (did not help).
• Actually escape the {e} in example – did not work either.
• \shorthandoff is a good recommendation from the already linked question and helps. Unfortunately, if you uncomment it above, it still does mangle with the e: We do not get ökay anymore, but we do get ëxample.
• This answer pointed me to \verb. While this works, this means I a) cannot put it in \texttt anymore, it refuses that, and worse, b) it prevents any line breaks at the line end.
Edit: It accidentally uses typewriter (same-width) font already, which I want in this case, but which may not be desired in other use cases.

¹ Actually, it seems to force a line break and showed this:

Argument of \language@active@arg" has an extra }.

I basically just want Markdown-like behaviour of the   .

In other words: How do I really escape " in LaTeX?

TexLive 2019

• move \shorthandoff{"} behind \begin{document}, or load babel with \usepackage[ngerman,shorthands=off]{babel}. Aug 29, 2020 at 14:12
• @UlrikeFischer Thanks, but wtf, why does that make a difference? Respectively, why does the placement in the preamble disable it for o, but not for e??? BTW, feel free to submit an answer to this question, so I can accept it and give you the credit for solving this.
– rugk
Aug 29, 2020 at 14:29
• lots of stuff is initialized at begin document. And for me both the ö and ë are there. Btw: the code from @egreg is naturally better, as he disables the shorthand only in the place where you really don't want it. Aug 29, 2020 at 14:36
• why not \verb|example_string_mentioned": "yes_no"| ? Aug 29, 2020 at 15:31
• no you mentioned verbatim in the question by which I assumed you meant \begin{verbatim} I did not understand your comment about not allowing \texttt as \verb uses the texttt font. Aug 29, 2020 at 17:36

You can disable shorthands locally with \languageshorthands{none}:

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\newcommand{\inlinecode}[1]{{\languageshorthands{none}\ttfamily #1}}

\begin{document}
This is a JSON snippet:
\inlinecode{"example\_string\_mentioned": "yes\_no"} \\
\inlinecode{"okay"} \\
\inlinecode{"yikes"}\\
\inlinecode{"ohno"}
\end{document}


• @DavidCarlisle ups ;-) Aug 29, 2020 at 15:45

Use an indirect method that changes the meaning of the active ", if it is active; if not, the code does nothing harmful.

\documentclass{article}
\usepackage[ngerman]{babel}

\newcommand{\inlinecode}{}% just to warn if it is already defined
\DeclareRobustCommand{\inlinecode}{%
% don't propagate this outside the argument
\begingroup
% define the active quote to yield a normal "
\begingroup\lccode~=" \lowercase{\endgroup\edef~}{\string"}%
% call an auxiliary command to do the typesetting and close the group
\doinlinecode
}
\newcommand{\doinlinecode}[1]{\texttt{#1}\endgroup}

% disable " making strange characters, it breaks our code quotations
% \shorthandoff{"} % does not fully work (see below)

\title{shorthandoff}
\date{August 2020}

\begin{document}

\maketitle

\section{Introduction}

This is a JSON snippet:
\inlinecode{"example\_string\_mentioned": "yes\_no"} \\
\inlinecode{"okay"} \\
\inlinecode{"yikes"}\\
\inlinecode{"ohno"}

\end{document}


• Oh, thanks. This looks complicated though, so I guess the solution by @UlrikeFischer is easier/less complicated.
– rugk
Aug 29, 2020 at 14:34

Even though the question focuses on the " character, it seems to really be about how to display inline verbatim (JSON) code.

The standard way to include inline verbatim text is with \verb, like

In our \textsc{json} we do this with \verb|"otter": 5|.


With verbatim characters are shown as themselves. That includes spaces, which means that there are no linebreaks. (Also these verbatim spaces are not stretchy as normal spaces are.)

Another way to include code is to use packages that are meant for that, like listings. That package includes commands for showing code listings given in the TeX file or taken from an external file, and also for short inline code, like this:

\documentclass{article}
\usepackage{listings}
\lstset{
basicstyle=\ttfamily\small,
}

\begin{document}
In our \textsc{json} we do this with \lstinline|"otter": 5|.
\end{document}


(Of course the basicstyle setting here is just an example.)

Both here and for \verb the delimiting | character can be some other character. Some popular choices are /, + and !, depending on what's in the text.

One big feature of a package like listings is to do code highlighting. It doesn't know about JSON syntax out of the box though. If you are interested in customizing that (even though it wasn't mentioned in the question), you can see for example this and this question.

More to the point here, there is an option to allow linebreaks. Just add

  breaklines=true,


to the \lstset settings.

There is nothing extra you need to do for the straight " character if you use normal typewriter font with the above. But you will find that the straight ' character is still shown as a curly quote! There is a package to avoid that in verbatim text. Add:

\usepackage{upquote}
`