我有一个问题不知道如何处理:
我需要添加一些小的代码单词 - 这些有时可能是某个东西的较长名称。为了显示这是一段代码的名称,我制作了一个小命令,用特殊字体\code{...}
包装。...
但是当文本...
太长时,它就会超出页面的边距。
梅威瑟:
\documentclass[a4paper]{article}
\usepackage{showframe} % Just to show the frames
%A font for code
\newcommand{\code}[1]{{\fontfamily{pcr}\selectfont #1}}
\begin{document}
This is a longer line -- notice what happens now at the end: \code{OfThisLongPeiceOfText}
\end{document}
有什么建议可以解决这个问题吗? \\
当我看到它时强制使用它会带来问题,或者将它与它分开\code{firstPart}\code{SecondPart}
?
答案1
在这种情况下使用:
\begin{sloppypar}
This is a longer line -- notice what happens now at the end: \code{OfThisLongPeiceOfText}
\end{sloppypar}
如果你不喜欢\sloppypar
使用:
\usepackage[scaled=0.85]{beramono}
\usepackage{showframe} % Just to show the frames
\newcommand{\code}[1]{{\ttfamily#1}}
或者使用德语快捷方式:
\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{libertine}
\usepackage[scaled=0.85]{beramono}
\usepackage[ngerman,english]{babel}
\useshorthands{"}
\addto\extrasenglish{\languageshorthands{ngerman}}
\usepackage{showframe} % Just to show the frames
\newcommand\code[1]{{\ttfamily#1}}
\begin{document}
This is a longer line -- notice what happens now at the end: \code{OfThis""Long""Peice""OfText}
\end{document}
答案2
一种方法(不是最好的)是将其与seqsplit
包拆分:
\documentclass[a4paper]{article}
\usepackage{showframe}
\usepackage{seqsplit}
\newcommand{\code}[1]{{\fontfamily{pcr}\selectfont \seqsplit{#1}}}
\begin{document}
This is a longer line -- notice what happens now at the end: \code{OfThsLongPeiceOfText}
\end{document}
但存在解决方案骆驼香烟盒分裂(厚颜无耻地取自https://tex.stackexchange.com/a/66603/31636正如@DavidCarlisle 提到的那样),它大多数时候都有效:
\documentclass[a4paper]{article}
\usepackage{showframe}
\showhyphens{createUnspecifiedNodeErrorMarker}
\makeatletter
\def\code{\leavevmode\begingroup
\let\ifcase\iftrue
\def\or##1{%
\catcode`##1\active\uccode`\~`##1\uppercase{%
\def~{\egroup\penalty2\hbox\bgroup\string##1}}}%
\@Alph{}%
\@code}
\def\@code#1{\textbf{\hbox\bgroup#1\egroup}\endgroup}
\makeatother
\begin{document}
This is a longer line -- notice what happens now at the end: \code{OfThisLongPeiceOfText}
\code{OfThisLongPeiceOfText}
\code{OfThisLongPeiceOfText}
\code{OfThisLongPeiceOfText}
\code{OfThisLongPeiceOfText}
\end{document}
但不总是: