语境:在我的文档中,我使用引用机制来引用个人参考资料。我定义了一个\citeref{}
宏来处理逗号分隔的参数列表。后者具有以下结构:abc_123
。宏的目的\citeref
是遍历参数列表,做一些黑魔法(MWE 中未表示),最后在方括号内打印参数列表。因为参考资料中有下划线,所以我\detokenize
将它们排版。
问题:-like 引用的连字符abc_123
不是最佳的。值得注意的是,下划线后没有启用换行符_
。然而,使用该hyphenat
包并不有效,因为字符串被去标记化了。
问题:如何在去标记化的字符串中的下划线后启用换行/连字符?
平均能量损失
\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}
答案1
使用url
包代替\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}