0

I'm using \spverb to show inline code but if there is a long code it doesn't wraping up properly since it only breaks at spaces.

But if I use \parabox with \textlength it works just fine. Is there a way to config spverb globally to use this size?

\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} 

Intended output is second one

enter image description here

6
  • 1
    Erh, your example does not compile. Please don't ignore compilation errors.
    – daleif
    Commented Feb 15, 2023 at 13:57
  • 1
    I would never expect anything verbatim-like to be usable inside the argument to a macro. Use the env version instead: \begin{minipage}{\textwidth} ... \end{minipage}.
    – daleif
    Commented Feb 15, 2023 at 13:58
  • Hey it's working fine. I'm using overleaf.
    – d4rkshad0w
    Commented Feb 16, 2023 at 3:44
  • If you are using texlive.net just remove \parabox example and it work's fine. Give me an example how to use an env correctly with size.
    – d4rkshad0w
    Commented Feb 16, 2023 at 4:14
  • With \parbox it is not workign fine, neither locally or in overleaf. As I said don't ignore compilation errors. Overleaf reports 3 compilation errors (though there is just one, the fact that you cannot use anything verbatim like inside the argument to a macro, unless it is very specially made, and \parbox is not).
    – daleif
    Commented Feb 16, 2023 at 8:35

1 Answer 1

2

Your code don't work (you obtain errors in the log), you could not have a \verb (or similar) in a \parbox, as explained by daleif.

With \begin{minipage}{\textwidth} ... \end{minipage} you obtain indeed (see the second block):

enter image description here

I don't know why in a minipage environment, the verbatim text is raggedright, however.

For the inline \spverb|...|, the problem is that in monospaced fonts, the width of space characters are fixed.

But, there are some workarounds (see below; perhaps there exists betters; note that halas, with the Latin Modern Mono Proportional font, quotes are curly, I don't know how to made it straight).

\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} 

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .