20

I am preparing a beamer presentation where I need to display some markdown code involving backticks. Specifically, I want to show backticks in the verbatim environment. However, LaTeX renders them as open quotation marks.

How can I ensure backticks in the verbatim environment display as backticks?

Here's a minimal example:

\begin{verbatim}
Example: `2 + 2`
\end{verbatim}

An ideal solution would not require me to change the code itself.

2
  • I don't know if it is an option for you but using LuaLaTeX/XeLaTeX with fontspec generate the right characters.
    – ArTourter
    Jul 15, 2012 at 10:22
  • Wow. User asks latex to: "relay these characters verbatim", and then latex transforms all delimiters to a 32-bit ISO-8859-1 Latin glyph set and replacing single quotes with variable angle diacritics. I'm starting to think I know why Latex is now a 6.5GB 90 minute install. Someone at the helm is having a laugh throwing sand in those gears. Verbatim must be interpreted in bizarro world which means: "Change everything to be something else, at random". Nov 15, 2019 at 21:56

3 Answers 3

20

You can also use the relative new package upquote, which is explicitly created for that task – together with the change for single quote marks, compare How to make a real apostrophe or single-quote in LaTeX.

\documentclass{article}
\usepackage{upquote}
\begin{document}
\begin{verbatim}
Example: `2 + 2`
\end{verbatim}
\end{document}

Without upquote:
Example without "upquote"

With upquote:
Example with "upquote"

3

It is possible to do this with a variation of the code found under a similar question here.

In your case, add the following to your preamble:

\makeatletter
\let\@sverbatim\@verbatim
\def\@verbatim{\@sverbatim \verbatimwithtick}
{\catcode``=13 \gdef\verbatimwithtick{\chardef`=18 }} 
\makeatother

In the verbatim environment this sets the definition of ` to correspond to the 18th character in the font table which is a back tick. Normally it corresponds to an opening quote.

1

With eTeX, @jpallen doesn't need auxiliary commands:

\documentclass{article}
\makeatletter
{\catcode`\`=13
\xdef\@verbatim{\unexpanded\expandafter{\@verbatim}\chardef\noexpand`=18 }
}
\makeatother

\begin{document}
\begin{verbatim}
Example: `2 + 2`
\end{verbatim}
\end{document}
2
  • 3
    {\catcode`\`=13 \g@addto@macro\@verbatim{\chardef`=18 }} seems less complicated. :)
    – egreg
    Jul 16, 2012 at 6:39
  • Eh, complicated? I can't see the complication!
    – Ahmed Musa
    Jul 16, 2012 at 15:55

You must log in to answer this question.

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