2

I am trying to print URL and other paths within a gray-colored box. However, I cannot do so. The moment I use \colorbox with \path, the line breaking stops working. It appears as if \colorbox stops \path from breaking lines; \color doesn't.

Is there a way to rectify it?

I tried reading through url.sty but my limited understanding of LaTeX could not decipher it. I tried going through the definitions of \path and \colorbox. Didn't understand it much though.

\path:
macro:->\leavevmode \begingroup \urlstyle {tt}\Url 


\colorbox:
macro:#1{->\protect \color@box {#1}{

\color@box {#1}{:
undefined

Here are two related (maybe) but different questions

  1. file ended while scanning use of \path
  2. Can I redefine a command to contain itself?

Here's my MWE

\documentclass{article}
\usepackage{letltxmacro}
\usepackage[obeyspaces]{xcolor}
\usepackage[obeyspaces]{url}
\usepackage{etoolbox}

\LetLtxMacro{\oldpath}{\path}
\renewcommand{\path}[1]{{\colorbox{gray!8}{\oldpath{#1}}}}

\newrobustcmd\pathcb[1]{\colorbox{gray!8}{\path{#1}}}


\begin{document}

\colorbox{gray!7}{\parbox{\dimexpr\linewidth-2\fboxsep}{\strut \path{https://tex.stackexchange.com/questions/77877/line-break-inside-a-verbhttps://tex.stackexchange.com/questions/77877/line-break-inside-a-verb}\strut}}

\pathcb{https://tex.stackexchange.com/questions/77877/line-break-inside-a-verbhttps://tex.stackexchange.com/questions/77877/line-break-inside-a-verb}

\path{https://tex.stackexchange.com/questions/77877/line-break-inside-a-verbhttps://tex.stackexchange.com/questions/77877/line-break-inside-a-verb}

\end{document}

I tried Werner's answer to breakable colorbox but the lines still won't break. I'd rather avoid TiKz, having absolutely no knowledge about it.

Do let me know if I can provide more details.

  • 1
    Well colorbox won't break across lines, and putting url's and pathes (which can contains specials chars like ~ or %) in the argument of some other command is normally not a good idea, if the command is not specially meant for verbatim material. Imho your best bet to get something working is tikzmarks at the begin and the end. But before trying to get something working: background color is a rather brutal method to emphasize. Do you really think it is a good idea to use it in running text? – Ulrike Fischer Jun 29 '17 at 13:05
  • Many websites display console commands and paths in a fixed width font and background color to emphasise it. Like so - I am a path. I was hoping to borrow this style – abyshukla Jun 30 '17 at 5:28
  • websites are different to printed text. When a link is hidden behind some text ("I am a link") you need to indicate that there is a link, but if the text is "www..." why emphasise? And looking at how tex.sx shows your question: The colored links are okay, the gray code block too, but the background color of the commands disrupts my reading. I would prefer here a tt-font with perhaps a bit color. – Ulrike Fischer Jun 30 '17 at 7:32
1

Commands such as \url and \path don't absorb their argument, but delay doing so in order to make several settings in between.

Add the color as a font attribute:

\documentclass{article}
\usepackage{xcolor}
\usepackage[obeyspaces]{url}
\usepackage{etoolbox}

\appto{\UrlFont}{\color{gray!70}}

\begin{document}

Some text for triggering a line break
\url{https://tex.stackexchange.com/questions/77877/line-break-inside-a-verb}
\url{https://tex.stackexchange.com/questions/77877/line-break-inside-a-verb}
and some text after the links

\end{document}

enter image description here

  • Thanks for the reply. What if I have to write a macro, which prints backslashes and all other special characters just like \path, but is in a colorbox and line-breaks appropriately (like a path)? – abyshukla Jun 30 '17 at 13:24
  • @manucpp Isn't it the same? – egreg Jun 30 '17 at 13:30
  • Yes, sorry. I had \url{} in my mind all this while. I will delete that comment. – abyshukla Jun 30 '17 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.