207

I need to output the below text but since \ is a special character, I cannot:

[RegularExpression(@"\d+")]

Also sometimes I need use dollar sign $ as well but it seems to be a special char, too.

What is the way to escape those in LaTeX?

UPDATE:

I used \verb as suggested but I am unable to run commands inside it. Also, in the output the font and the opacity of the text is different than the default one:

\verb|[RegularExpression(\newline @"\d+")]|
4

6 Answers 6

314

The following ten characters have special meanings in (La)TeX:

& % $ # _ { } ~ ^ \

Outside \verb, the first seven of them can be typeset by prepending a backslash; for the other three, use the macros \textasciitilde, \textasciicircum, and \textbackslash.

\documentclass{article}

\begin{document}

\& \% \$ \# \_ \{ \}

\textasciitilde

\textasciicircum

\textbackslash

\end{document}

enter image description here

Note that the seven "single non-letter" macros don't gobble the space following them. For the last three that do gobble up the space after them you can try one of these methods to add space.

8
  • In pdfLaTeX I have defined a new command \textampersand by using \newcommand*{\textampersand}[0]{\&} or \renewcommand*{\textampersand}[0]{\&}. In XeLaTeX it is already available, I think.
    – matth
    Commented May 9, 2012 at 17:35
  • In terms of preventing characters gobbling the space after them, you can use \usepackage{xspace} and then \xspace directly after the character that might gobble the space to prevent this.
    – Savara
    Commented Dec 1, 2015 at 12:24
  • 3
    It's worth adding that [ (and ]) also have a special meaning in certain circumstances. For example \item [a]bc will not print [a]bc (as it will be considered as an option passed to the command).
    – kebs
    Commented Nov 11, 2018 at 8:00
  • yea but if you surround \textbackslash within \texttt it does not have the right "code font" Commented Dec 19, 2019 at 15:19
  • 1
    @antman Sure, my point was only to point out that [ and ] have to be considered carefully, thus they can be considered as having a "special meaning".
    – kebs
    Commented Oct 16, 2022 at 18:41
34

Usually text like that is typeset in typewriter type and so there's a slick way to arrange it

\verb|[RegularExpression(@"\d+")]|

After \verb should go a character that's not used in the text to print "verbatim" and the same character should follow the text.

This command has a drawback: it can't be used in the argument of other commands.

There's a second "solution" which can come handy if it's needed a limited number of times:

\texttt{[RegularExpression(@"\string\d+")]}

where commands inside the argument to \texttt are allowed. It's not even necessary to use \texttt:

\textsf{[RegularExpression(@"\string\d+")]}

will work as well (when T1 font encoding is active) and will print the string in sans serif type.

5
  • 2
    first solution is nice but I cannot use commands inside it. second solution breaks my entire document. Does every single little thing have to be so hard in LaTeX :)
    – tugberk
    Commented Nov 12, 2011 at 16:22
  • Would you please modify your question and show some more cases?
    – egreg
    Commented Nov 12, 2011 at 16:27
  • sure, see it. I updated!
    – tugberk
    Commented Nov 12, 2011 at 16:31
  • 2
    @egreg: in your definition of \pseudoverb, you've got [1} instead of [1]. And a drawback you don't mention of \detokenize is that it inserts spaces after macros names with more than one character (not sure if it's relevant for regular expressions). Commented Nov 12, 2011 at 22:11
  • @PhilippeGoutet Yes, you're right; I've deleted that part
    – egreg
    Commented Nov 12, 2011 at 22:14
13

I needed a way to escape all special characters and I found this Perl function:

sub latex_escape {
  my $paragraph = shift;

  # Replace a \ with $\backslash$
  # This is made more complicated because the dollars will be escaped
  # by the subsequent replacement. Easiest to add \backslash
  # now and then add the dollars
  $paragraph =~ s/\\/\\backslash/g;

  # Must be done after escape of \ since this command adds latex escapes
  # Replace characters that can be escaped
  $paragraph =~ s/([\$\#&%_{}])/\\$1/g;

  # Replace ^ characters with \^{} so that $^F works okay
  $paragraph =~ s/(\^)/\\$1\{\}/g;

  # Replace tilde (~) with \texttt{\~{}}
  $paragraph =~ s/~/\\texttt\{\\~\{\}\}/g;

  # Now add the dollars around each \backslash
  $paragraph =~ s/(\\backslash)/\$$1\$/g;
  return $paragraph;
}

For example it will convert this:

& % $ # _ { } ~ ^ \ \today

into this:

\& \% \$ \# \_ \{  \} \texttt{\~{}} \^{} $\backslash$ $\backslash$today
0
4

I was surprised the use of \detokenize was not shown, but I then noted that egreg had it at one point until Phillipe pointed out that

And a drawback you don't mention of \detokenize is that it inserts spaces after macros names with more than one character (not sure if it's relevant for regular expressions).

The tokcycle package, as part of its examples document provides an \altdetokenize which, despite its name, is not an alternate way to detokenize. Rather, it takes detokenized output and reformulates its appearance to remove those ugly spaces that are inserted after macros, when it is appropriate to do so (through strategically placed \unskips).

In the MWE, first \detokenize, then \altdetokenize as described in the tokcycle examples document: https://ctan.org/tex-archive/macros/generic/tokcycle. Note that T1 font encoding must be active for backslashes to render properly.

This is an interesting macro in that, unlike the recommended tokcycle approach of collecting the processed tokens from the input in a token list and then outputting the token list at the end, this macro works in real-time, outputting its result during, rather than after, the token cycle. This is possible because the output is detokenized, and I don't have to worry about macros in the input stream demanding arguments that have not yet been processed.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tokcycle}
\newif\ifmacro
\newcommand\altdetokenize[1]{\begingroup\stripgroupingtrue\macrofalse
  \stripimplicitgroupingcase{-1}%
  \tokcycle
    {\ifmacro\def\tmp{##1}\ifcat\tmp A\else\unskip\allowbreak\fi\macrofalse\fi
     \detokenize{##1}\ifx##1\bgroup\unskip\fi\ifx##1\egroup\unskip\fi}
    {\ifmacro\unskip\macrofalse\fi\{\processtoks{##1}\ifmacro\unskip\fi\}\allowbreak}
    {\tctestifx{\\##1}{\\}{\ifmacro\unskip\allowbreak\fi
     \allowbreak\detokenize{##1}\macrotrue}}
    { \hspace{0pt plus 3em minus .3ex}}
    {#1}%
  \unskip
\endgroup}

\begin{document}
\detokenize{[RegularExpression(@"\d+")]}

\altdetokenize{[RegularExpression(@"\d+")]}
\end{document}

enter image description here

2

Just in case anyone is looking for another way to escape special characters, this can be done with the listings package.

\lstinline{[RegularExpression(@"\d+")]}

enter image description here

Since all characters will be displayed as they are, it is not possible to execute any command inside the block.

Update: the inline listing \lstinline supports the math mode by including the mathescape option. It also supports the escapechar option, but it needs to be activated manually (more here).

% enable escapechar for \lstinline
\makeatletter
\patchcmd{\lsthk@TextStyle}{\let\lst@DefEsc\@empty}{}{}{\errmessage{failed to patch}}
\makeatother

% ...

\lstinline[escapechar=`]|\cite{} in \lstinline `\cite{wgan}`|
\lstinline[basicstyle=\small\ttfamily,mathescape]{[RegularExpression(@"\d+")] // some math: $\sum_{i}^{N}{x_i}$}

Note that \lstinline{ } does not escape {}, so \lstinline| | is used.

enter image description here

It can also be customized:

\lstinline[basicstyle=\small\ttfamily\color{red}]{[RegularExpression(@"\d+")]}

where \ttfamily is the teletypefont family.

enter image description here

2

A note:

\textasciitilde and \textasciicircum are unnecessarily verbose. Use \^{} and \~{} instead.


Some clarification is needed.

\^ and \~ themselves are not individual "characters" like \% \_ or \# are, but rather they're command to "place an accent on the following character", like how \'a gives á.

So there must be "something" ({}) that follows the \^ -- in particular {\^} will not work. You can think of this as putting ^ on an empty base.

Nevertheless, in all cases, \^{} and \~{} gives the exact same result as typesetting the character at slot 94 and 126 from the font file directly.

You can test it yourself by compiling this document:

%! TEX program = pdflatex
\documentclass{article}
%\usepackage[T1]{fontenc}  % optional
\begin{document}
\pagenumbering{gobble}

\texttt{\textasciicircum\^{}\symbol{94}\^a\^b}

\textasciicircum\^{}\symbol{94}\^a\^b

\texttt{\textasciitilde\~{}\symbol{126}\~a\~b}

\textasciitilde\~{}\symbol{126}\~a\~b

%{\^}  % does not work!

\end{document}

Output: output

This is because the command is implemented (after some composite-character table lookup) with TeX's primitive \accent command, but:

TeX by topic

so \accent ⟨accent⟩ ⟨nothing follows⟩ gives just \char ⟨accent⟩.


Another note:

In OT1 font encoding, \texttt{...} used with _{}\ does not give the best result.

Refer to:

\documentclass{article}
\usepackage[OT1]{fontenc}
\begin{document}
\pagenumbering{gobble}

\texttt{\& \% \$ \# \_ \{ \} \textbackslash}

\texttt{\& \% \$ \# \symbol{95} \symbol{123} \symbol{125} \symbol{92}}

\end{document}

Output: output

There are a few ways to fix it:

Refer to my answer in https://tex.stackexchange.com/a/643285/250119 for an explanation what's going on.

You must log in to answer this question.

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