如何全局设置 textwidth 为 spverb

如何全局设置 textwidth 为 spverb

我正在使用\spverb它来显示内联代码,但如果代码很长,它就无法正确换行,因为它只会在空格处中断。

但如果我使用\parabox\textlength,它就正常工作。有没有办法全局配置 spverb 以使用此大小?

\documentclass{article}
\usepackage[margin=1.25in]{geometry} 
\setlength{\parindent}{0pt}
\setlength{\parskip}{\baselineskip}
\usepackage{spverbatim}
\usepackage{adjustbox}
\begin{document} 

\spverb|println!("A "Hello, World!" program is generally a computer program that ignores any input and outputs or displays a message similar to "Hello, World!".");println!("A "Hello, World!" program is generally a computer program that ignores any input and outputs or displays a message similar to "Hello, World!".");|

\parbox[t]{\textwidth}{
\spverb|println!("A "Hello, World!" program is generally a computer program that ignores any input and outputs or displays a message similar to "Hello, World!".");println!("A "Hello, World!" program is generally a computer program that ignores any input and outputs or displays a message similar to "Hello, World!".");|
}
\end{document} 

预期输出是第二个

在此处输入图片描述

答案1

您的代码不起作用(您在日志中收到错误),您不能\verb在 a 中使用 a(或类似的)\parbox,正如 daleif 所解释的那样。

\begin{minipage}{\textwidth} ... \end{minipage}你确实获得(见第二块):

在此处输入图片描述

但是,我不知道为什么在小型页面环境中,逐字文本会变得参差不齐。

对于内联\spverb|...|,问题在于等宽字体中空格字符的宽度是固定的。

但是,有一些解决方法(见下文;也许存在更好的方法;请注意,使用拉丁现代单色比例字体,引号是卷曲的,我不知道如何将其弄直)。

\documentclass{article}
\usepackage[margin=1.25in]{geometry} 
\setlength{\parindent}{0pt}
\setlength{\parskip}{\baselineskip}
\usepackage[english]{babel}
\usepackage[pangram]{blindtext}
\usepackage{spverbatim}
\usepackage{fvextra}
\begin{document} 

\blindtext

% As the paragraph is justified, there is space for the beginning of the word "any",
% but no hyphenation available in verbatim, and spaces have fixed width.
% So you obtain Overfull \hbox
\spverb|println!("A "Hello, World!" program is generally a computer program that ignores any input and outputs or displays a message similar to "Hello, World!".");println!("A "Hello, World!" program is generally a computer program that ignores any input and outputs or displays a message similar to "Hello, World!".");|

% Here, there is spaces with variable width for the first line, so, the first line 
% is correctly justified. No variable spaces available for the next lines,
% so you obtain Overfull \hbox for the next lines
If you have some words at the begin: \spverb|println!("A "Hello, World!" program is generally a computer program that ignores any input and outputs or displays a message similar to "Hello, World!".");println!("A "Hello, World!" program is generally a computer program that ignores any input and outputs or displays a message similar to "Hello, World!".");|. It's better on lines with some available rubber lengths (here only the first line).

% With the package fvextra, you could ask for breaklines, but as in the first example,
% there is no variable space available, and you ask for a justified text
\Verb[breaklines]{println!("A "Hello, World!" program is generally a computer program that ignores any input and outputs or displays a message similar to "Hello, World!".");println!("A "Hello, World!" program is generally a computer program that ignores any input and outputs or displays a message similar to "Hello, World!".");}

% Here, you allow the line breaking anywhere, so it's OK. Note the (customizable)
% symbol at the end of lines. You can also add a customizable symbol a the begin
% of the line after the break
\Verb[breaklines, breakanywhere]{println!("A "Hello, World!" program is generally a computer program that ignores any input and outputs or displays a message similar to "Hello, World!".");println!("A "Hello, World!" program is generally a computer program that ignores any input and outputs or displays a message similar to "Hello, World!".");}

% Here, in a raggedright paragraph, there is no problem. If the word doesn't fit
% in the current line, it goes in the next line.
% the output is like the minipage environment (but you are still in a inline verbatim)
\begin{flushleft}
\Verb[breaklines]{println!("A "Hello, World!" program is generally a computer program that ignores any input and outputs or displays a message similar to "Hello, World!".");println!("A "Hello, World!" program is generally a computer program that ignores any input and outputs or displays a message similar to "Hello, World!".");}   
\end{flushleft}

% Here we change the tt font to a proportional (but looks like the former tt)
\renewcommand*\ttdefault{lmvtt} % uses Latin Modern Mono proportional

% Generally, no problem if you uses proportional fonts (the spaces have variable width)
\spverb|println!("A "Hello, World!" program is generally a computer program that ignores any input and outputs or displays a message similar to "Hello, World!".");println!("A "Hello, World!" program is generally a computer program that ignores any input and outputs or displays a message similar to "Hello, World!".");|

\Verb[breaklines]{println!("A "Hello, World!" program is generally a computer program that ignores any input and outputs or displays a message similar to "Hello, World!".");println!("A "Hello, World!" program is generally a computer program that ignores any input and outputs or displays a message similar to "Hello, World!".");}

% But on some complicated situations, it goes wrong (I replaced "and outputs"
% by "andouputs".) The first line is OK, but not the second.
\spverb|println!("A "Hello, World!" program is generally a computer program that ignores any input andoutputs or displays a message similar to "Hello, World!".");println!("A "Hello, World!" program is generally a computer program that ignores any input and outputs or displays a message similar to "Hello, World!".");|

\end{document} 

在此处输入图片描述

相关内容