6

I'm trying to get long inline code to wrap at points that I specify with \-. I've got it working almost perfectly with this:

% !TEX program = xelatex

\documentclass[fontsize=11pt,pagesize=auto,hidelinks,cleardoublepage=empty,parskip]{book}

\usepackage{listings}
\usepackage{xcolor}
\usepackage{relsize}

\usepackage[paperheight=10in,paperwidth=6in,margin=2cm,heightrounded,bindingoffset=5mm,showframe=false]{geometry}

\sloppy

\newcommand*{\code}{\lstinline[basicstyle=\relscale{0.9}\ttfamily\color{red},keywordstyle=\color{red},stringstyle=\color{red},keepspaces=true,breaklines=true,literate={\\\-}{}{0\discretionary{\textrm{-}}{}{}}]}

\begin{document}

Here is a paragraph with some inline code. \code{Really\-Long\-Name\-First\-Second\-Third\-Fourth\-Fifth}.

\end{document}

Output:

enter image description here

My last remaining goal is to have the hyphen appear black rather than red, so it appears distinct from the code. I thought I'd just be able to use textcolor like this:

\newcommand*{\code}{\lstinline[basicstyle=\relscale{0.9}\ttfamily\color{red},keywordstyle=\color{red},stringstyle=\color{red},keepspaces=true,breaklines=true,literate={\\\-}{}{0\discretionary{\textcolor{black}{\textrm{-}}}{}{}}]}

However, this gives me:

Improper discretionary list

I've tried various things, like wrapping the entire discretionary piece in {}, but nothing I've tried has worked.

How can I make the hyphen black?

10

Instead of using \textcolor inside a discretionary, you can use it around the discretionary:

\newcommand*{\code}{\lstinline[basicstyle=\relscale{0.9}\ttfamily\color{red},keywordstyle=\color{red},stringstyle=\color{red},keepspaces=true,breaklines=true,literate={\\\-}{}{0\textcolor{black}{\rmfamily\discretionary{-}{}{}}}]}

Are you aware of the \lstset macro? Instead of using your \code, you can write

\lstset{basicstyle=\relscale{0.9}\ttfamily\color{red},keywordstyle=\color{red},stringstyle=\color{red},keepspaces=true,breaklines=true,literate={\\\-}{}{0\textcolor{black}{\rmfamily\discretionary{-}{}{}}}}

in your preamble. Then \lstinline always uses the options.

  • Wonderful, thanks! I'm at the point where I don't even question why one way works when another doesn't now. Regarding using lstset and lstinline, I just find \code reads (and writes) far nicer. – Kent Boogaart Mar 12 '18 at 7:15
  • 1
    For the question why: In \discretionary only characters, boxes and kerns are allowed, but \textcolor inserts a whatsit. – Marcel Krüger Mar 12 '18 at 7:18
  • @MarcelKrüger whether I was sure or not doesn't really matter, but certainly I was wrong:-) I'll clean up the comments here and fix my answer, thanks (who implemented \textcolor I wonder....) – David Carlisle Mar 12 '18 at 16:32
5

\textcolor colour whatsits to change the colour which are not allowed in a discretionary, however you can have boxes so you just need to put the content of each field in an \hbox

enter image description here

\documentclass{article}

\usepackage{color}

\begin{document}

aaaaaaaaaaaaaaaa\discretionary
{\hbox{\textcolor{red}{AAA}}}{\hbox{\textcolor{blue}{BBB}}}{\hbox{\textcolor{green}{CCC}}}%
bbbbb
aaaaaaaaaaaaaaaa\discretionary
{\hbox{\textcolor{red}{AAA}}}{\hbox{\textcolor{blue}{BBB}}}{\hbox{\textcolor{green}{CCC}}}%
bbbbb
aaaaaaaaaaaaaaaa\discretionary
{\hbox{\textcolor{red}{AAA}}}{\hbox{\textcolor{blue}{BBB}}}{\hbox{\textcolor{green}{CCC}}}%
bbbbb
\end{document}

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.