[Tex/LaTex] How to print escape characters in LaTeX?

symbols

How can I print \n, \t, \v and so on in LaTeX document? I mean I want in my output two chars: \n not a new line.

I tried:

  1. \\n
  2. \verb!\\n!

With no effects…

Best Answer

Use the first version.
Both the I and II version show other ways which are unnecessary complex (and exploit certain TeXnicalities).

Code

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\newcommand*{\escape}[1]{\texttt{\textbackslash#1}}
\newcommand*{\escapeI}[1]{\texttt{\expandafter\string\csname #1\endcsname}}
\newcommand*{\escapeII}[1]{\texttt{\char`\\#1}}
\begin{document}
    We escape \escape{n}, \escape{t} and \escape{v}.

    We escape \escapeI{n}, \escapeI{t} and \escapeI{v}.

    We escape \escapeII{n}, \escapeII{t} and \escapeII{v}.

    We escape \verb!\n!, \verb!\t! and \verb!\v!
\end{document}

Output

enter image description here

Related Question