0

I'm trying to print a vertical bar symbol, but it's \active in the ltxdoc document style (this code doesn't compile):

\documentclass{ltxdoc} \begin{document} This is a vertical bar: |\|| \end{document} 

I want the bar to be inside |...| formatting, which essentially is \texttt{...} in this document style. What would be a solution?

4
  • Actually tex.stackexchange.com/a/41475/250119 works although is clumsy. Commented Nov 18, 2022 at 4:57
  • It works, but it renders the bar in a different font. It's similar to $\vert$. Also clumsy, as you said :( Commented Nov 18, 2022 at 5:09
  • 1
    Okay then, easiest way to understand is \usepackage[T1]{fontenc} \texttt{ab\textbar cd}. Anything else (that I can think of) involves a nontrivial amount of TeX programming/understanding. Commented Nov 18, 2022 at 5:17
  • This is exactly what I was looking for! Please, make it an answer, I will accept it. Commented Nov 18, 2022 at 5:21

2 Answers 2

1

An easy-to-understand solution is

%! TEX program = pdflatex \documentclass{ltxdoc} \usepackage[T1]{fontenc} % pdflatex only!! in other engine omit this line or specify encoding TU % in pdflatex without this line the vertical bar would not be in typewriter font \begin{document} \texttt{ab\textbar cd} \end{document} 

You will not be able to use |ab\textbar cd| here because it's verbatim and will print \textbar verbatim (nevertheless |ab\textbar cd| can be used inside another command's argument if it's a "real argument" but I don't recommend relying on this inconsistent behavior, would be confusing)


For the sake of overcomplicating things, this is a solution that allows you to escape the | by doubling it.

%! TEX program = pdflatex \documentclass{ltxdoc} \ExplSyntaxOn \makeatletter \char_set_catcode_active:N \| \AtBeginDocument{ \char_set_active_eq:NN \| \__usersixdigits_vertical_bar_active_do:w } \char_set_catcode_other:N \| \cs_new_protected:Npn \__usersixdigits_vertical_bar_active_do:w { \begingroup \let\do\@makeother \dospecials \__usersixdigits_grab_following_argument:w } \cs_new_protected:Npn \__usersixdigits_grab_following_argument:w #1 | { \endgroup % reset the catcode of the following character (which might not be |) \texttt{#1} % typeset the text collected so far % the following peek will tokenize the following argument, so it's important to |\endgroup| above \peek_meaning_remove:NT \__usersixdigits_vertical_bar_active_do:w % we assume nothing other than the active | has this meaning { \texttt{|} % assuming the current font has the | character in the correct slot (which means either T1/TU encoding or OT1 typewriter font) \__usersixdigits_vertical_bar_active_do:w } } \makeatother \ExplSyntaxOff \begin{document} some text |12||34| vertical bar |||| vertical bar |||| abc ab|x||y|cd ab|x||y|\{cd \end{document} 

Output is as you expect.

output image

2

The bar is only a shorthand for \verb. So you can simply use \verb directly with as usual some delimiter which is not in the text:

\documentclass{ltxdoc} \begin{document} This is a vertical bar: \verb+|+ \end{document} 

Side remark: inside |...| formatting is the wrong concept. The shorthand is not about formatting (which can be changed), but allows verbatim typesetting. If you want to format in typewriter, use \texttt and \textbar (\| wouldn't work regardless if the bar is active or not, as \| is a math command). And if you want the bar to work like \texttt you will have to redefine it first.

1
  • This also works, except in command arguments where you can... apparently \verb|12\textbar 3| works. Commented Nov 18, 2022 at 9:47

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.