使用 \verb 命令时的连字符

使用 \verb 命令时的连字符

\verb我在使用命令时遇到问题。

通常,当我想要当前行(而不是新段落)中的一些逐字字体时,我会使用命令\verb,而不是verbatim环境。

因此,当我使用类似\verb"HereSomeLongText"不连字符的操作时,我的文本就会脱离页边距。

如何在里面使用连字符\verb?请注意,我想要内联(在同一行内)逐字文本。

这里有一些最小(非)工作示例,您可以在其中看到所描述的问题。

\documentclass[a4paper,10pt]{article}
\usepackage[english]{babel}
\usepackage[left=2cm, right=2.2cm,height=25cm]{geometry}

\DeclareFontFamily{OT1}{cmtt}{\hyphenchar \font=`\-}

\begin{document}
\noindent Here is some TeX example of text  that looks nice, but when 
I Want to type some text like  \verb"myDataFileLongName.csv" using the
verb command, there is no hyphenation nor like break adequately, so the
text inside the verb command get out of the page margins.

\\[0.25cm] So I would appreciate so much any help in order to solve this 
problem. Thanks, thanks,  thanks,  thanks,  thanks,  thanks,  thanks, 
thanks,  thanks,  thanks,  thanks,  thanks,  thanks,  thanks,  thanks,  
thanks,  thanks,  thanks,  thanks.
\end{document}

答案1

我不建议对文件名使用连字符,因为这会产生歧义:连字符是否属于该名称?

您可以考虑使用包\path提供的命令url,通过补充允许它在大写字母前中断的代码,例如:

\documentclass[a4paper,10pt]{article}
\usepackage[english]{babel}
\usepackage[left=2cm, right=2.2cm,height=25cm]{geometry}
\usepackage{url,etoolbox}
\appto\UrlSpecials{%
  \do\F{\penalty0 \mathchar`\F }%
  \do\L{\penalty0 \mathchar`\L }%
  \do\N{\penalty0 \mathchar`\N }%
}
\begin{document}
Here is some TeX example of text  that looks nice, but when
I Want to type some text like  \path{myDataFileLongName.csv} using the
verb command, there is no hyphenation nor like break adequately, so the
text inside the verb command get out of the page margins.

\end{document}

我没有添加所有大写字母,如果你愿意的话可以这样做,或者根据你的实际情况判断真正的需求是什么。

在此处输入图片描述

\path命令(以及\url)巧妙地利用了 TeX 的数学模式功能。完整描述其工作原理会太长;对于此应用程序,我们可以使用列表\UrlSpecials宏;列表中包含的字符(本地)数学激活,因此它们的出现会触发用规定的替换文本进行替换。

使用建议的代码,F将变成\penalty0 \mathchar`\F(说\penalty0 F会进入无限循环,但\mathchar`\F很安全):惩罚将允许换行F

如果不想全局修改命令\path,可以\UrlSpecials只在新命令的工作范围内本地添加到列表中。

\documentclass[a4paper,10pt]{article}
\usepackage[english]{babel}
\usepackage[left=2cm, right=2.2cm,height=25cm]{geometry}
\usepackage{url,etoolbox}

\let\breaktt\path
\patchcmd{\breaktt}
  {\begingroup}
  {\begingroup\apptourlspecials}
  {}{}
\newcommand{\apptourlspecials}{%
  \appto\UrlSpecials{%
    % add to this list
    \do\F{\penalty0 \mathchar`\F }%
    \do\L{\penalty0 \mathchar`\L }%
    \do\N{\penalty0 \mathchar`\N }%
  }%
}
\begin{document}
Here is some TeX example of text  that looks nice, but when
I Want to type some text like  \breaktt{myDataFileLongName.csv} using the
verb command, there is no hyphenation nor like break adequately, so the
text inside the verb command get out of the page margins.

\end{document}

该命令\path扩展为\leavevmode\begingroup\urlstyle{tt}\Url,因此我们定义\breaktt来执行相同操作,但我们还会在 之后添加一些内容\begingroup,这正是我们需要的列表的补充\UrlSpecials。这样\path和 的\url行为将与之前一样。

答案2

您可以使用\texttt而不是来强制连字符\verb

\documentclass[a4paper,10pt]{article}
\usepackage[UKenglish]{babel}
\begin{document}
Here is some TeX example of text that looks nice, but when
I type \verb!Sonification! using the
verb command, there is no hyphenation nor like break adequately, so the
text inside the verb command get out of the page margins.

Here is some TeX example of text that looks nice, but when
I type \texttt{Soni\-fi\-cation} using the
verb command, there is no hyphenation nor like break adequately, so the
text inside the verb command get out of the page margins.
\end{document}

在此处输入图片描述

相关内容