19

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.

Jeromy Anglim
  • 7,450
  • 6
  • 46
  • 63
  • I don't know if it is an option for you but using LuaLaTeX/XeLaTeX with fontspec generate the right characters. – ArTourter Jul 15 '12 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". – Eric Leschinski Nov 15 '19 at 21:56

3 Answers3

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"

Speravir
  • 19,491
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.

jpallen
  • 149
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}
Ahmed Musa
  • 11,742