5

I am a lazy guy and like to use newcommands often. After finalizing I am going to replace with the original code.

I'd like to have an abbreviation for the \verb|.. text goes here .. |

But something is going wrong here - I don't know how to fix my code:

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{xcolor}

\newcommand\bv{\verb|} \newcommand\ev{|}

\begin{document}

  1. Putting \color{green!40!black} \verb| hurz | \color{black} works

but:

  1. Putting \bv hurz \ev fails.

\end{document}

Since I am not a \TeX wizzard, but a beginner, every hint is highly appreciated!

I forgot: working with PDFLateX on Linux.

Regards!

Sebastiano
  • 54,118

3 Answers3

6

verbatim commands need to find the end string without expanding commands, so you can't hide it in a \ev command.

You can use fancyvrb to define short verbatim, see the documentation:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{xcolor}
\usepackage{fancyvrb}

\DefineShortVerb{|}

\begin{document} Putting |\section{abc} hurz| work \end{document}

Ulrike Fischer
  • 327,261
5

\verb has to know where to finish, but in the meantime it has to disable every special interpretation of characters, including the backslash.

One might think to delimit the material with the string \ev, but this would be very cumbersome. Delimiting with a single character not in the material that is to be printed verbatim is much simpler. But this works in a very indirect way.

When LaTeX finds \verb it examines the next character and changes its category code to mean “end-of-group”. Thus, upon finding another instance of it, the end-of-group nature of the character will automatically disable all the special settings needed for verbatim mode. What's the problem in defining \ev to be |? First of all, this | cannot be changed to mean “end-of-group”, but this is the least of the issues: \ev would be examined as a string made of three normal characters, because \verb is in force! So it cannot be interpreted as a command.

If your aim is to color inline verbatim material, there are simpler ways. I show two: one with a single color, defining a \greenverb macro with the same syntax as \verb. The second method defines \colorverb that takes as argument a color specification and then calls a generic color verbatim command.

\documentclass{article}
\usepackage{xcolor}
\usepackage{newverbs}

\newverbcommand{\greenverb}{\color{green!40!black}}{}

\begin{document}

Putting \greenverb|hurz| works

\end{document}

enter image description here

If you need several colors, you can define a generic command and then specialize it. Please, use a more detailed prefix than my (your name, a random string or whatever) in order to minimize the possibility of conflicts.

\documentclass{article}
\usepackage{xcolor}
\usepackage{newverbs}

\makeatletter % allow private control sequences \newverbcommand{\my@colorverb}{\color{\my@verbatimcolor}}{} \newcommand{\colorverb}[1]{\def\my@verbatimcolor{#1}\my@colorverb} \makeatother

\newcommand{\greenverb}{\colorverb{green!40!black}} \newcommand{\blueverb}{\colorverb{blue!80!black}}

\begin{document}

Putting \greenverb|hurz| works

Putting \blueverb|hurz| works

Putting \colorverb{red!80!black}|hurz| works

\end{document}

enter image description here

egreg
  • 1,121,712
3

This isn't a true verbatim, but it comes pretty close. For example, brace balancing is required of the input.

Syntax is what the user requested: \bv....\ev. Furthermore, the verbatim material can include line and paragraph (empty line) breaks.

One place it will produce improper output is if a \bv input line ends in a lone backslash \. Another improper output will arise if the catcode-1,2 characters have been changed...they will continue to be presented as { and }, unless additional steps are taken.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern,tokcycle}
\tokcycleenvironment\myverb
{\addcytoks[1]{\string##1}}
{\addcytoks{\{}\processtoks{##1}\addcytoks{\}}}
{\ifx\par##1\addcytoks{\\}\else\addcytoks[1]{\string##1}\fi}
{\addcytoks[1]{\string##1}}
\makeatletter
\def\bv{\begingroup\obeylines\catcode`\%=12%
  \long\def\tokencycle##1##2##3##4{\begingroup\let\endtokencycle
  \endtokcycraw\aftertokcycle{\the\cytoks\expandafter\endgroup
  \expandafter\endgroup\expandafter\tcenvscope
  \expandafter{\the\cytoks}}\tokcycraw{##1}{##2}{##3}{##4}}%
  \myverb}
\makeatother
\let\ev\endtokcycraw
\begin{document}
\textit{here we} go:
\bv ABc. D &^$% \xyz
\ {ef}g~\\

H#I^_ \ev back t\v{o} normal \today

a b% c \end{document}

enter image description here