0

I have the following override:

% override the inline code styling to show a gray highlight box
\let\oldTexttt\texttt
\renewcommand{\texttt}[1]{%
  \begin{RaggedRight}%
  \sethlcolor{codegray}%
  \ttfamily\hl{#1}%
  \end{RaggedRight}%
}

But I still have words that are supposed to be in the gray box getting split between lines. The words are commands like: selectpicture and listpictures. In one case the first word is split sel at the end of one line and ectpicture at the start of the next. In another case the second word is split listpic at the end of one line and tures at the start of the next. I need to have these words not getting split.

NOTE: I am not trying to prevent all word wrapping/line breaking throughout the document, just the ones in \texttt.

  • Hi, welcome to the site. It is always better if you provide your problems as a minimal, but compilable document (minimal working example, MWE). This way others see what class and packages they need to take into account and can also test there solution with no extra effort required. – Martin Scharrer Dec 14 '18 at 6:23
1

I figured it out. I needed to put an \mbox in there.

Problem solved.

  • 4
    Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. - From Review – naphaneal Dec 13 '18 at 20:54
  • 1
    @naphaneal: While short, this is still a acceptable self-answer, while (for me obviously) does not belong to the question post. – Martin Scharrer Dec 14 '18 at 6:21
  • 1
    @JonBelanger: Note that with \mbox you don't need RaggedRight anylonger, as it forces restricted horizontal mode and left/right adjustments are only for paragraph mode. – Martin Scharrer Dec 14 '18 at 6:25
  • @MartinScharrer didn't notice that. will pay more attention next time. – naphaneal Dec 16 '18 at 16:22
1

Your \begin{RaggedRight}...\end{RaggedRight} does nothing at all, except limiting the scope of \ttfamily, because \end{RaggedRight} just ends the scope where \RaggedRight is in force. The paragraph will be formed in exactly the same way with or without \RaggedRight.

I cannot recommend redefining \texttt. If you want to do it nonetheless, then use the proper way:

\documentclass{article}
\usepackage{soul,ragged2e,xcolor,letltxmacro}

\colorlet{codegray}{black!30}

\LetLtxMacro\oldTexttt\texttt % useless?
\let\texttt\relax

\DeclareRobustCommand{\texttt}[1]{%
  \mbox{\sethlcolor{codegray}\ttfamily\hl{#1}}%
}

\begin{document}

We have some nonsense text in order
to show what happens \texttt{abc def ghi jkl} and if this
splits across lines we're happy.

\end{document}

Adding \mbox makes line breaks impossible between items in the argument.

However, that's not what \texttt is expected to do. A different macro would be better for this and other reasons, mainly that a new name would add semantics to your typescript.

\documentclass{article}
\usepackage{soul,ragged2e,xcolor,letltxmacro}

\colorlet{codegray}{black!30}

\DeclareRobustCommand{\code}[1]{%
  \mbox{\sethlcolor{codegray}\ttfamily\hl{#1}}%
}

\begin{document}

We have some nonsense text in order
to show what happens \code{abc def ghi jkl} and if this
splits across lines we're happy.

\end{document}
  • thanks for the answer above. I have one issue where long commands do not wrap and run off the side of the page. I need to be able to prevent hyphenating, but allow for word wrapping. Is there an easier solution for this? – JonBelanger Dec 19 '18 at 14:00

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.