1

I'm looking to get a \verb string to output with bold font style as well as word wrapping. The closest I've come is by using the commented code rather than the currently live one. I realize I'm breaking the cardinal rule of never using \BF. I couldn't repeat the results with \textbf nor with \bfseries. Any thoughts on that?

\documentclass{article}
\usepackage{lmodern,verbatim}

\makeatletter \newenvironment{boldverb} {\def@xobeysp{\ } %{\def@xobeysp{\bf \ } \verbatim\rightskip=0pt plus 6em\relax} {\endverbatim} \makeatother

\begin{document}
    \begin{boldverb}
        \ \verb|\hspace{20pt}\newpageOkay, so where do we start, right? \textbf{Why is it that I can bold this stuff} but not \verb|
    \end{boldverb}
\end{document}

Even if I'm allowed in this case to use \bf, the beginning of the string is always going to have to be a backslash. Is there a latex argument that recognizes elements such as Word-Initial or Word-Final?

JTShakes
  • 13
  • 3
  • Welcome to TeX.SE! – Mensch Feb 08 '24 at 18:05
  • Welcome! It's not really clear what you want to do: using \verb inside a verbatim environment will do nothing at all except printing \verb. – egreg Feb 08 '24 at 18:17
  • You seem to understand. I'm looking to utilize \verb, but I'm also looking for a way to manipulate the font style of the text. I just realized now that even when I get the space, all the remaining backslashes turn into quotes. I can settle for not being able to get the output with word wrapping, but I want a different font style. – JTShakes Feb 08 '24 at 18:56
  • The verbatim environment is in the preamble so I can manipulated the \verb output in a way to revise certain font attributes. That's the closest way I found to do it. – JTShakes Feb 08 '24 at 18:59
  • Maybe something like https://tex.stackexchange.com/questions/299401/bold-just-one-line-inside-of-lstlisting? Possibly combined with https://tex.stackexchange.com/questions/27663/using-bold-italic-text-inside-listings if you want a monospaced font for the verbatim. – Marijn Feb 08 '24 at 19:10

2 Answers2

0

While \detokenize is not the same as verbatim, it may suffice, depending on whether your input follows certain rules (e.g., balanced braces, no %, etc.). Making it bold is straightforward.

\documentclass{article}
\usepackage[T1]{fontenc}
\begin{document}
\textbf{\detokenize{            \ \verb|\hspace{20pt}\newpageOkay, so where do we start, right? \textbf{Why is it that I can bold this stuff} but not \verb| 
\bgroup{} blah blah\egroup}}
\end{document}

enter image description here

0

Not sure what your aim is.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{fvextra}

\DefineVerbatimEnvironment{boldverb}{Verbatim}{ fontfamily=\rmdefault, fontshape=\updefault, fontseries=bx, breaklines, breakanywhere, breaksymbolleft=, breakindent=0pt, }

\begin{document}

\begin{boldverb} \ \verb|\hspace{20pt}\newpageOkay, so where do we start, right? \textbf{Why is it that I can bold this stuff} but not \verb| \end{boldverb}

\end{document}

enter image description here

egreg
  • 1,121,712
  • It all started out because I wanted to explain how I came out with a command \verb+\enquote{\blash paragraph{\blash hspace{20pt!}}.}+ but couldn't get it in bold, so I had to write it out as "\verb|\textbf{$\backslash$verb+$\backslash$enquote{$\backslash$paragraph{$\backslash$hspace$\backslash${20pt!}}.}+.}| \par" I like your preamble. Is it something that could be placed in a .sty, or is it like \makeatletter? FYI, that code won't make any sense in your editor, as it has newcommands in it – JTShakes Feb 09 '24 at 11:21
  • This actually solved another issue I was have with \enquote! Wow. You totally nailed it. The preamble is quite elegant. I look forward to studying it! – JTShakes Feb 09 '24 at 11:26