17

On any SE post one can use the backtick symbol ("`") in order to mark some text as being a piece of code, or computer output. For example: void main().

Is there a way to make use of "`" the same way in a LaTeX document?

  • 5
    How will you type opening quotation marks? – cfr Mar 30 '17 at 0:26
  • 11
    It's code formatting, not highlighting. Markdown doesn't have anything to denote highlighting, unless you count *emphasis* and the like. – David Z Mar 30 '17 at 3:27
  • 11
    Please do not use code formatting (backticks, four-space paragraph indents, pre tags) for “highlighting” or any other kind of visual effect. Its primary purpose is not visual, but semantic, it tells the computer that the text is supposed to be code. Most visual browsers render that as a monospace font with gray background, but alternative browsers, like screen readers for the blind, have to do something to allow text to be understood as code. This can make the text very difficult to understand as prose—reading letter-by-letter is not unheard of. Please be considerate of accessibility. – KRyan Mar 30 '17 at 12:59
  • FYI the symbol you show "`" is more correctly called a back-tick. A single quote mark is actually "'". I'm not sure what keyboard Vancouverites use, AFAIK it can be hard to type one or both on some layouts (typing English on a French layout, argh!). They are different characters though and mean different things :) – Toby Mar 30 '17 at 14:13
  • 1
    Related, as pertains to italic, bold, and bold-italic via *, **, and ***: tex.stackexchange.com/questions/236439/… – Steven B. Segletes Mar 31 '17 at 10:12
24

In addition to Werner's answer, the same is possible with the listings package. To undefine the shorthand use \lstDeleteShortInline`.

Be aware, that using ` as an escape character for inline code will most likely clash with language feature packages such as babel.

\documentclass{article}
\usepackage{listings}
\lstset{basicstyle=\ttfamily}
\begin{document}
\lstMakeShortInline`

Here is some inline code `\int f(x) \, \mathrm{d}x`

\end{document}

enter image description here

16

You can define your own verbatim shorthand using fancyvrb's \DefineShortVerb:

enter image description here

\documentclass{article}

\usepackage{fancyvrb}
\DefineShortVerb{\`}

\begin{document}

On any SE post one can use the quote in order to highlight a text.
For example, `this text is highlighted`.

\end{document}

To remove this functionality, use \UndefineShortVerb{\`}, as it may interfere with other uses of ` within your code.

  • Small bug: error with this solution \DefineShortVerb{`} when also using \usepackage{graphicx}: ! Missing number, treated as zero. <to be read again> \let l.30 \chardef\mptopdfstoredatcode\catcode \@ ? ! Emergency stop.. If graphicx` is not loaded, no error... – Basj Sep 11 '18 at 10:13
  • 1
    @Basj: ...then use \AtBeginDocument{\DefineShortVerb{`}}. – Werner Sep 11 '18 at 16:19
9

It is more natural to use " or | rather than ` for this purpose, since ``foo'' is translated to “foo” in LaTeX.

There are several packages to define a short verbatim command for this. The basic one is shortvrb, which is part of LaTeX base package doc:

\documentclass{article}

\usepackage{shortvrb}
\MakeShortVerb|
% \DeleteShortVerb|

\begin{document}

Short verbatim: |foo#bar|

\end{document}

As Werner and Henri Menke said, you can also use fancyvrb, gmverb, newverbs for this functionality, or use listings with syntax highlight support.

Sometimes, however, you don't want to use verbatim text, only special fonts or color is needed. Then you can use some tricks to define a one-character command yourself:

\documentclass{article}

\newcommand\MakeShortHighlight[2][\texttt]{%
  \begingroup\lccode`\~=`#2\lowercase{\endgroup
    \def~##1~{#1{##1}}}%
  \catcode`#2=\active}
\newcommand\DeleteShortHightlight[1]{%
  \catcode`#1=12 }

\usepackage{xcolor}

\begin{document}

\MakeShortHighlight\"
Short highlighted: "typewriter text"


\MakeShortHighlight[\textcolor{red}]\|
Short highlighted: |red color|

\end{document}

enter image description here

5

Yes, you can use the back-ticks and the indentation of four spaces as in markdown, using well ... the markdown package:

mwe

\documentclass[a5paper,12pt]{article}
\setlength{\parindent}{0pt}
\setlength{\parskip}{0em}
\usepackage{markdown}
\begin{document}
\begin{markdown}
A minimal working example

    \documentclass{article}
    \def\foo#1{#1} 
    \begin{document}
    \foo{bah}
    \end{document}

The macro `\def\foo#1{#1}` does not anything   
\end{markdown}
\end{document}
  • This should be the most upvoted answer! – CarLaTeX Apr 5 '17 at 4:48
2

This doesn't perfectly answer your question, but I prefer the use of commands to perform the action you desire. Perhaps someone else will find this useful.

\documentclass{article}

\newcommand{\code}[1]{\texttt{\detokenize{#1}}}

\begin{document}

This command allows you to type \code{void main()} inline in your paragraphs.

\end{document}

example output

Disclaimer: This command does not handle pound signs (#) properly. Please feel free to comment if you know how to extend this command to handle pound signs (bonus points if it's without any dependencies!).

  • You could ask a question for the pound sign problem! – CarLaTeX Apr 5 '17 at 4:46
  • There are more things this command does not handle properly. Try for example \code{100%} or \code{ single { brace } – Henri Menke Jun 22 '17 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.