2

Context: In my document, I use a citation mechanism for personal references. I defined a \citeref{} macro that processes a comma-separated list of arguments. The latter have following structure: abc_123. The purpose of the \citeref macro is to iterate over the list of arguments, do some black magic (not represented in the MWE), and finally print the list of arguments within squared brackets. Because there is an underscore in the references, I \detokenize them in order to typeset them.

Problem: The hyphenation of the abc_123-like references is not optimal. Notably, no line break is enabled after the underscore _. Yet using the hyphenat package is not effective, as the string is detokenized.

Question: How to enable line break/hyphenation after an underscore inside a detokenized string?


MWE

enter image description here

\documentclass[a5paper]{scrartcl}
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}

    \usepackage{etoolbox}
    \newcommand{\citeref}[1]{
        % cf. https://tex.stackexchange.com/a/87423/64454; https://tex.stackexchange.com/q/367418/64454
        [%
            \def\nextitem{\def\nextitem{, }}% Separator
            \forcsvlist\citerefitem{#1}% Process list
        ]%
    }
    \newcommand{\citerefitem}[1]{%
        \nextitem
        \detokenize{#1}%
    }

    \usepackage{hyphenat}
\begin{document}
    Usually, it works fine: either for single \dots\dots\dots\dots\citeref{abcabc_123} or for multiple references \dots\dots\dots\dots\citeref{abc_123, def_456}.

    However, for longer references, a hyphenation would be welcome after the underscore: \citeref{abcdefghijklmnopqrstuvwxyz_12345678910111213}.
\end{document}
lockstep
  • 250,273
ebosi
  • 11,692
  • Note: I could change abc_123 into abc\_123: this would enable me to use hyphenat as is and not use \detokenize. However, I don't want this solution as my "black magic" notably consists in writing this reference into an external file for later processing. – ebosi Jul 16 '17 at 15:21
  • Are you tied to etoolbox? – egreg Jul 16 '17 at 15:26
  • My only constraint is that I'm close to the deadline of my thesis (hence the tracking of overfull boxes) (-;. – ebosi Jul 16 '17 at 15:28
  • @egreg The most critical part of the "black magic" is only a \immediate\write\tempfile{#1}. So I suppose any other way to process a comma-separated list would be ok. – ebosi Jul 16 '17 at 15:30
  • I found some recent code for an answer to a question of yours; it would be very easy to change it so that instead of \_ the code substitutes something like \_\allowbreak – egreg Jul 16 '17 at 15:45
  • @egreg Which question are you referring to? As far as I've understood \usepackage{hyphenat} + \_ = \_\allowbreak. However, I would prefer not change my abc_123 into abc\_123 (or abc\_\allowbreak 123), as I would like to have the string abc_123 to be written in my temporary file without backslash... – ebosi Jul 16 '17 at 15:54
  • https://tex.stackexchange.com/a/367414/4427 – egreg Jul 16 '17 at 15:56

1 Answers1

2

Use the url package instead of \detokenize.

\documentclass[a5paper]{scrartcl}
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}

    \usepackage{etoolbox}
    \usepackage{url}
    \DeclareUrlCommand\citerefurl{\urlstyle{same}}
    \newcommand{\citeref}[1]{
        % cf. https://tex.stackexchange.com/a/87423/64454; https://tex.stackexchange.com/q/367418/64454
        [%
            \def\nextitem{\def\nextitem{, }}% Separator
            \forcsvlist\citerefitem{#1}% Process list
        ]%
    }
    \newcommand{\citerefitem}[1]{%
        \nextitem
        \citerefurl{#1}%
    }

    \usepackage{hyphenat}
\begin{document}
    Usually, it works fine: either for single \dots\dots\dots\dots\citeref{abcabc_123} or for multiple references \dots\dots\dots\dots\citeref{abc_123, def_456}.

    However, for longer references, a hyphenation would be welcome after the underscore: \citeref{abcdefghijklmnopqrstuvwxyz_12345678910111213}.
\end{document}

enter image description here

Henri Menke
  • 109,596