TransWikia.com

How to putting the pipe | as a working command into a macro newcommand

TeX - LaTeX Asked by user164525 on April 19, 2021

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}


newcommandbv{verb|}
newcommandev{|}

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


but: 


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

3 Answers

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}

Answered by Ulrike Fischer on April 19, 2021

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]{defmy@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

Answered by egreg on April 19, 2021

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}
tokcycleenvironmentmyverb
{addcytoks[1]{string##1}}
{addcytoks{{}processtoks{##1}addcytoks{}}}
{ifxpar##1addcytoks{}elseaddcytoks[1]{string##1}fi}
{addcytoks[1]{string##1}}
makeatletter
defbv{begingroupobeylinescatcode`%=12%
  longdeftokencycle##1##2##3##4{begingroupletendtokencycle
  endtokcycrawaftertokcycle{thecytoksexpandafterendgroup
  expandafterendgroupexpandaftertcenvscope
  expandafter{thecytoks}}tokcycraw{##1}{##2}{##3}{##4}}%
  myverb}
makeatother
letevendtokcycraw
begin{document}
textit{here we} go:
bv ABc. D &^$% xyz
 {ef}g~

H#I^_ ev back tv{o} normal today

a
b%
c
end{document}

enter image description here

Answered by Steven B. Segletes on April 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP