7

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...

Mensch
  • 65,388
Katie
  • 315
  • 7
    \textbackshlash n works. This can be wrapped inside \texttt{\textbackslash n}. \verb!\n! should also work, but not in a parameter of a macro. (Also \string\n exists.) \\ is already reserved for many “new line” purposes. If you often use these things, create a new macro: \newcommand*{\escape}[1]{\texttt{\textbackslash #1}} which you can use then as \escape{n}. – Qrrbrbirlbel Apr 15 '13 at 15:25
  • Welcome to TeX.sx! Usually, we don't put a greeting or a “thank you” in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you. – jub0bs Apr 15 '13 at 15:29
  • 1
    @Qrrbrbirlbel: Why don’t you post your comment as an answer?! – Tobi Apr 15 '13 at 15:34
  • Agree, duplicate. I've flagged it as such. – Sverre Apr 15 '13 at 15:37
  • 1
    Isn't it supposed to be \verb"\n" rather than \verb!\n!? The former works for me… – MickG Sep 19 '14 at 17:39

1 Answers1

9

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

Qrrbrbirlbel
  • 119,821