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?

yegor256
  • 12,021
  • Actually https://tex.stackexchange.com/a/41475/250119 works although is clumsy. – user202729 Nov 18 '22 at 04:57
  • It works, but it renders the bar in a different font. It's similar to $\vert$. Also clumsy, as you said :( – yegor256 Nov 18 '22 at 05: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. – user202729 Nov 18 '22 at 05:17
  • This is exactly what I was looking for! Please, make it an answer, I will accept it. – yegor256 Nov 18 '22 at 05:21

2 Answers2

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.

Ulrike Fischer
  • 327,261
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

user202729
  • 7,143