2
\begin{table}
\begin{tabular}{l | l | c | c }
Name     & Hex      & dezimal     & C-char \\
<cr>     & 0x0D     & 13          & '\\r' \\     % should read '\r'
<lf>     & 0x0A     & 10          & '\\n' \\
\end{tabular}
\end{table}

Escaping the '\' preceding 'n' or 'r' to e.g. '\\n' inserts a new line - as the '\\' at the end of the line does. Using '\n' or '\r' produces weird characters. How can this be coded properly?

peets
  • 121
  • 3

5 Answers5

7

I will be happy to remove this. As mentioned above, to get a backslash, you can use \textbackslash. So you may want

\documentclass{article}
\begin{document}
\begin{table}
\begin{tabular}{l | l | c | c }
Name     & Hex      & dezimal     & C-char \\
\texttt{<cr>}     & \texttt{0x0D}     & \texttt{13}          &
\texttt{\textbackslash r} \\   
\texttt{<lf>}     & \texttt{0x0A}     & \texttt{10}          &
\texttt{\textbackslash n} \\
\end{tabular}
\end{table}
\end{document}

enter image description here

or

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}

\begin{document}
\begin{table}
\begin{tabular}{>{\ttfamily}l >{\ttfamily}l >{\ttfamily}c >{\ttfamily}c }
\toprule
\multicolumn{1}{l}{Name}     & \multicolumn{1}{l}{Hex}      & 
\multicolumn{1}{c}{dezimal}     & \multicolumn{1}{c}{C-char} \\
\midrule
<cr>     & 0x0D     & 13          &
\textbackslash r \\   
<lf>     & 0x0A     & 10          &
\textbackslash n \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

enter image description here

6

You can use \textbackaslash as already suggested, but I'd use typewriter type in some columns and angle brackets rather than less or greater than signs.

Also, special commands are better to avoid littering the code with ad hoc hacks.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{booktabs}
\usepackage{array}

\newcommand{\lowascii}[1]{$\langle$#1\/$\rangle$}
\newcommand{\esc}[1]{\textbackslash\symbol{`#1}}

\begin{document}

\begin{tabular}{l >{\ttfamily}c c >{\ttfamily}c}
\toprule
Name & 
\multicolumn{1}{c}{Hex} &
dezimal &
\multicolumn{1}{c}{C-char} \\
\midrule
\lowascii{cr}  & 0x0D & 13 & '\esc{r}'  \\
\lowascii{lf}  & 0x0A & 10 & '\esc{n}'  \\
\textbackslash & 0x5C & 92 & '\esc{\\}' \\
\bottomrule
\end{tabular}

\end{document}

I assume that escape sequences consist of single characters. If you, for example, want to escape % you type \esc{\%}.

enter image description here

egreg
  • 1,121,712
  • "Littering" would be a good description for your choice of language. Could you perhaps consider avoiding such disrespectful and misleading language, in particular when there are answers around that have superior and cleaner codes? One may almost think you are referring to them. –  Jun 05 '20 at 19:41
  • @Schrödinger'scat I don't see answers with superior code. Yours, in particular, although it has no hack. And you're being rude. – egreg Jun 05 '20 at 20:04
  • I just ask you not to use such language. You may continue to think that your codes are great and answers by others are not, that's your choice. However, using wording in the way you are doing is not helpful, but irritating. If you had something useful to communicate, you certainly would not need to use language as you do. (And yes, introducing macros like \esc which have the potential to lead to a lot of complications if one works on a file in a collaboration is IMHO not a good choice, but I am only mentioning that because you bring this up.) –  Jun 05 '20 at 20:10
4

Although Scrödinger's cat's answer is more robust because does not use any verbatim command (which can create problems if you use the tabular in a moving argument), another option is

  1. switch the whole table to \ttfamily (in a group)
  2. use \rmfamily for the headers
  3. use \verb where needed.
\documentclass{article}
\usepackage[T1]{fontenc}
\begin{document}
\begin{table}
    \centering
    {\ttfamily
        \begin{tabular}{l | l | c | c }
            \rmfamily Name      & \rmfamily Hex & \rmfamily dezimal  &\rmfamily  C-char \\
            <cr>     & 0x0D     & 13           & \verb|\r| \\     % should read '\r'
            <lf>     & 0x0A     & 10           & \verb|\n| \\
        \end{tabular}%
    }
    \caption{Caption}
\end{table}
\end{document}

enter image description here

Rmano
  • 40,848
  • 3
  • 64
  • 125
3

One more method using the current font type with \detokenize. With pdflatex will need the T1 encoding (do not work with OT1). The MWE also load babel to use single guillemots.

mwe

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{ccccc}\toprule
Name & Hex & dezimal & C-char \\\midrule
\guilsinglleft cr\guilsinglright & 0\texttimes0D & 13 & \detokenize{\r} \\ 
\guilsinglleft lf\guilsinglright & 0\texttimes0A & 10 & \detokenize{\n} \\\bottomrule
\end{tabular}
\end{document}
Fran
  • 80,769
0
\begin{table}
    \begin{tabular}{l | l | c | c }
        Name     & Hex      & dezimal     & C-char \\
        <cr>     & 0x0D     & 13          & \textbackslash r \\     % should read '\r'
        <lf>     & 0x0A     & 10          & \textbackslash n \\
    \end{tabular}
\end{table}

enter image description here

js bibra
  • 21,280
  • 6
    You need to include the instruction \usepackage[T1]{fontenc} in the preamble in order for pdfLaTeX to typeset the characters < and > correctly in text mode. – Mico Jun 04 '20 at 08:13