1

Searching through various packages documentation and this site I have found how to write inline verbatim text, that is (when neccessary) broken at the end of line. For this feature, I am using \Verb{} command provided by package fvextra. This works perfectly well, but I would like to achieve also highlighting the verbatim text, still preserving the automatic line-breaking.

With text, that doesnt require too much escaping, I am effectively using command \texttt{}, which is highlightable by command \hl{} provided by soul package.

Can the same effect be achived also for text with \Verb{} command?

MWE:

\documentclass [a4paper, 12pt, twoside, openright] {scrbook}
\usepackage{fontspec}

\usepackage [left=2.5cm, right=2cm, bottom=3cm, headheight=15.3pt] {geometry}       

\usepackage[dvipsnames,x11names,svgnames,table]{xcolor}

\usepackage{fvextra}
\fvinlineset{breaklines,%
   breakafter=\space ,
   breakanywhere
}

\usepackage[htt]{hyphenat}

\usepackage{soul}
\usepackage{soulutf8}

\sethlcolor{Snow2}

\begin{document}

Test of \Verb{\Verb{}} command working at the end of line \Verb{text that should go on as long as it is forced to be broken at the end of line}.

Test of \Verb{\texttt{}} command working at the end of line, that is also highlighted \hl{\texttt{text that should go on as long as it is forced to be broken at the end of line}}.

There is some text to make sure that argument of command \Verb{\Verb{}} will be broken  \hl{{\Verb{Text in Verb}}, but the highlighting with soul doesnt work.
\end{document}
1

fvextra's \Verb{...} can be used inside other commands, unlike \verb and some other verbatim commands. When this is done, \Verb behaves like \texttt, except that it performs a lot of analysis so that almost all characters will work except for #, %, unpaired curly braces {}, multiple adjacent spaces, double carets ^^, and some situations involving backslashes. So the easiest solution for most cases is to define a new command wrapping \Verb and \hl, perhaps something like this in the preamble:

\makeatletter
\newcommand{\HLVerb}[1]{%
  \edef\hverb@tmp{#1}%
  \expandafter\hl\expandafter{\hverb@tmp}}
\makeatother

If you need a broader range of characters than what \Verb can handle when wrapped in another command, you might consider using \EscVerb instead, which makes the backslash into an escape for non-alphanumeric ASCII characters.

| improve this answer | |
  • Hello Mr. Poore, thank you for your answer. Your suggested \newcommand definition works for my MWE perfectly, but my MWE maybe wasnt constructed properly (and has been then misleading). If I put ANY TeX special character as argument of \HLVerb it is by TeX recognised as such (those are mostly underscores _) which leads to TeX failing compilation and looking for inline math ... The same happens if I use \EscVerb in simillar manner. I still feel as TeX beginner (regular user?), so even for educational purposes I would like to ask you what does this newcommand definition do? – Tomáš Kruliš Jan 2 at 19:42
  • I understand first two lines. In Wiki, I found that \edef is defining new command with expansion of next command sequence. But I cant find what is the original definition of command \hverb@tmp{#1} ? I will also work on understanding \expandafter; what I found out yet means that first is exapnded second \expandafter ; then \hl{exapansion product} is expanded too ... ? If my follow-up comments are not entirely clear, I edit my question with MWE that would display my situation more specifically. – Tomáš Kruliš Jan 2 at 19:57

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.