我有一个很长的单词,里面有加号(例如 This+Is+A+Long+Word),它经常会进入页面的边缘。和 都\sloppy
解决\emergencystretch 3em
了这个问题,但它们也会在行中添加很多空格。我更希望 Latex 能够在符号处换行+
,但不使用连字符。
为了让事情稍微复杂一点,我将这个词定义为一个命令,这样它总是斜体(即用\newcommand{\myword}{\textit{This+Is+A+Long+Word}}
)。这还要求我在单词后留一个空格时使用~
(如果有一个简单的解决方法,那就太好了),尽管我尝试过只删除 ,但这~
并没有什么区别。
答案1
不确定我是否完全理解了您的“不带连字符”请求。在这里,我让+
换行符消失:
\documentclass[a4paper]{article}
\newcommand{\plussy}[1]{%
\begingroup
\itshape
\begingroup\lccode`~=`+\lowercase
{\endgroup\def~}{\discretionary{}{}{+}}%
\catcode`+=\active
\scantokens{#1\noexpand}%
\endgroup
}
\usepackage{lipsum}
\begin{document}
If an unrecoverable error occurs during the transformation, then
\plussy{This+Is+A+Long+Word}
\end{document}
如果您希望保留+
,但允许在此处换行,
\documentclass[a4paper]{article}
\newcommand{\plussy}[1]{%
\begingroup
\itshape
\begingroup\lccode`~=`+\lowercase
{\endgroup\def~}{\discretionary{+}{}{+}}%
\catcode`+=\active
\scantokens{#1\noexpand}%
\endgroup
}
\usepackage{lipsum}
\begin{document}
If an unrecoverable error occurs during the transformation, then
\plussy{This+Is+A+Long+Word}
\end{document}
在上面的代码中,+
换行符总是出现在行尾。如果你希望它总是出现在下一行的行首,那么
\documentclass[a4paper]{article}
\newcommand{\plussy}[1]{%
\begingroup
\itshape
\begingroup\lccode`~=`+\lowercase
{\endgroup\def~}{\discretionary{}{+}{+}}%
\catcode`+=\active
\scantokens{#1\noexpand}%
\endgroup
}
\usepackage{lipsum}
\begin{document}
If an unrecoverable error occurs during the transformation, then
\plussy{This+Is+A+Long+Word}
\end{document}
答案2
如果您有这种形式的“单词”,则可以为代表它们的命令定义一个基础结构:
\documentclass{article}
\NewDocumentCommand{\breakingplus}{}{\discretionary{+}{}{+}}
\ExplSyntaxOn
\NewDocumentCommand{\defineplusword}{mm}
{% #1 = command name, #2 = text
\tl_set:Nn \l_tmpa_tl { #2 }
\tl_replace_all:Nnn \l_tmpa_tl { + } { \breakingplus }
\cs_new:Npx #1 { \exp_not:V \l_tmpa_tl }
}
\ExplSyntaxOff
\defineplusword{\myword}{This+Is+A+Long+Word}
\begin{document}
Let's try and write a paragraph that's long enough so \myword\ ends up near the margin.
\parbox{0pt}{\hspace{0pt}\myword}
\end{document}
您还可以为这种形式的一次性单词定义一个命令。
\documentclass{article}
\NewDocumentCommand{\breakingplus}{}{\discretionary{+}{}{+}}
\ExplSyntaxOn
\NewDocumentCommand{\defineplusword}{mm}
{% #1 = command name, #2 = text
\tl_set:Nn \l_tmpa_tl { #2 }
\tl_replace_all:Nnn \l_tmpa_tl { + } { \breakingplus }
\cs_new:Npx #1 { \exp_not:V \l_tmpa_tl }
}
\NewDocumentCommand{\plusword}{m}
{
\group_begin:
\tl_set:Nn \l_tmpa_tl { #1 }
\tl_replace_all:Nnn \l_tmpa_tl { + } { \breakingplus }
\tl_use:N \l_tmpa_tl
\group_end:
}
\ExplSyntaxOff
\defineplusword{\myword}{This+Is+A+Long+Word}
\begin{document}
Let's try and write a paragraph that's long enough so \myword\ ends up near the margin.
Let's try and write a paragraph that's long enough so \plusword{This+Is+A+Long+Word}
ends up near the margin.
\parbox{0pt}{\hspace{0pt}\myword}
\end{document}
您可以看到单词可以在 + 字符处跨行拆分。您还可以看到如何在 后添加空格\myword
,即不是通过输入 来实现~
。或者,使用\myword{}
。
用于\parbox
表明每个 + 字符都是一个可能的换行点。
如果您希望这些单词为斜体,但“+”字符为直立,
\documentclass{article}
\NewDocumentCommand{\breakingplus}{}{\discretionary{\textup{+}}{}{\textup{+}}}
\ExplSyntaxOn
\NewDocumentCommand{\defineplusword}{mm}
{% #1 = command name, #2 = text
\tl_set:Nn \l_tmpa_tl { #2 }
\tl_replace_all:Nnn \l_tmpa_tl { + } { \breakingplus }
\cs_new:Npx #1 { \exp_not:N \textit{\exp_not:V \l_tmpa_tl} }
}
\NewDocumentCommand{\plusword}{m}
{
\group_begin:
\tl_set:Nn \l_tmpa_tl { #1 }
\tl_replace_all:Nnn \l_tmpa_tl { + } { \breakingplus }
\textit{\tl_use:N \l_tmpa_tl}
\group_end:
}
\ExplSyntaxOff
\defineplusword{\myword}{This+Is+A+Long+Word}
\begin{document}
Let's try and write a paragraph that's long enough so \myword\ ends up near the margin.
Let's try and write a paragraph that's long enough so \plusword{This+Is+A+Long+Word}
ends up near the margin.
\end{document}
答案3
如果您能够在 XeLaTeX 下编译您的文档,该fontspec
软件包可以让您临时或“动态”重新定义连字符 - 比如,重新定义+
字符 - 并自动切换到字体的斜体。
以下示例提供了一个名为 的用户宏,\PlusWord
该宏将其参数斜体化并使用+
字符作为连字符。请注意,斜体校正也会自动应用。
% !TEX TS-program = xelatex
\documentclass{article}
\setlength\textwidth{1.1cm} % small value for \textwidth parameter
\setlength\parindent{0pt} % just for this example
\usepackage{fontspec}
% Define a new "font family": same as default (Latin Modern),
% but with italic shape and with 'HyphenChar={+}'.
\newfontfamily{\PlusHyphenChar}{lmroman10-italic}%
[Path=/usr/local/texlive/2023/texmf-dist/fonts/opentype/public/lm/,
HyphenChar={+}]
\newcommand\PlusWord[1]{{\PlusHyphenChar #1\/}} % the user macro
\begin{document}
This+Is+A+Long+Word
\textit{This+Is+A+Long+Word}
\smallskip
uu \PlusWord{This+Is+A+Long+Word} uu
\end{document}
答案4
为什么不直接使用\babelhyphen{+}
?
\documentclass[twocolumn,a5paper]{article}
\usepackage[english]{babel}
\begin{document}
\newcommand\+{\babelhyphen{+}}
\newcommand{\youword}{\textit{This\+Is\+A\+Long\+Word}}
\newcommand{\myword}{This Is A Long Word}
\myword{}
\youword{}
\myword{}
\youword{}
\myword{}
\youword{}
\myword{}
\youword{}
\end{document}