5

I have a \n in my code which is not compiling correctly in verbatim environment.

\documentclass[]{article}
\usepackage{fancyvrb}
\usepackage{color}

\begin{document}

\begin{Verbatim}[commandchars = \\\{\}]
"A very very \n long title"     \textcolor{red}{A very very long title}
\end{Verbatim}

\end{document}

I am trying to get ''A very very \n long title''. And the sentence after it in red color

How to fix this.

4

The question is a bit unclear but I think you want

enter image description here

\documentclass[]{article}
\usepackage{fancyvrb}
\usepackage{color}

\begin{document}

\begin{Verbatim}[commandchars = !\{\}]
"A very very \n long title"     !textcolor{red}{A very very long title}
\end{Verbatim}

\end{document}
  • Yes, this is exactly what I want. – An student Jan 4 '18 at 15:43
3

When using commandchars in fancyvrb, the resulting characters are executed/processed as (La)TeX characters rather than being set verbatim. You've set \, { and } to be treated as such. Therefore, any control sequence (starting with a \) will have to be defined.

In your case, \textcolor exists (from color), but \n does not. You can define \n to set \string\n (output similar to \n):

enter image description here

\documentclass{article}

\usepackage{fancyvrb,xcolor}

\newcommand{\n}{\string\n}

\begin{document}

\begin{Verbatim}[commandchars = \\\{\}]
"A very very \n long title"     \textcolor{red}{A very very long title}
\end{Verbatim}

\end{document}

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.