给出以下自定义命令:
\newcommand{\topic}[1]{#1\index{#1}}
\newcommand{\class}[1]{\textsf{\topic{#1}}}
用于具有完整包名的 Java 类,例如:
\class{org.openscience.cdk.layout.StructureDiagramGenerator}
使用当前命令定义,类名在句点处不会使用连字符。我该如何更改 \newcommand{} 以让 LaTeX 在句点处使用连字符连接类?
我在 \caption{} 方法中使用了 \class{},这似乎使某些解决方案无效。
答案1
我倾向于使用该url
包来完成这项任务。(它为此类内容的换行提供了灵活的选项,而不仅仅是 URL。)以下是示例:
\documentclass[twocolumn]{article}
\usepackage{url}
\DeclareUrlCommand\printtopic{\urlstyle{sf}}
\newcommand{\topic}[1]{\protect\printtopic{#1}\index{#1}}
\DeclareRobustCommand{\class}[1]{\topic{#1}}
\begin{document}
text foo bar baz hello
\class{org.openscience.cdk.layout.StructureDiagramGenerator}
\end{document}
答案2
无论你做什么,几乎都不可能得到一个包含所有类名变体的可接受的排版。你可以使用半手动方法,如下例所示(最后一个例子)。
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{url}
\begin{document}
\overfullrule=0.5pt
\parskip=20pt
Using \textbackslash url, it will remove all spaces the colon will be placed at end of section, whereas a common convention is to move it at the next line
\fbox{\begin{minipage}{1.65in}
\url{org.openscience.cdk.layout.StructureDiagramGenerator}
\end{minipage}}
\gdef\classformatter#1{
\hyphenpenalty=-200
\textsf{#1}
}
It is impossible for \TeX to hyphenate the next example, as the fist word of a paragraph is never hyphenated and this is treated as one long word (\TeX\ is probably thinking it is just a long abbreviation).
\begin{minipage}{1.65in}
\classformatter{org.openscience.cdk.organizationlayout.StructureDiagramGenerator}
\end{minipage}
If you type a space at the point you want to allow a break \TeX\ will attempt to hyphenate,
\fbox{\begin{minipage}{1.65in}
\classformatter{org.openscience.cdk .organizationlayout .StructureDiagramGenerator}
\end{minipage}}
You will never avoid a little bit of hand manipulation, allow a break now and then;
\fbox{\begin{minipage}{1.65in}
\classformatter{org.openscience.cdk\\ .organizationlayout .StructureDiagramGenerator}
\end{minipage}}
\end{document}