5

My minimal file is:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{ragged2e}
\usepackage{lipsum}
\usepackage{pbox}
\begin{document}
\begin{Verbatim}[numbers=left,fontsize=\bf,commandchars=\^\#\*,baselinestretch=1,firstnumber=1,stepnumber=1]
    \begin{document}
    ^justifying^pbox#12cm*#The flowers of most species have five petals, with the exception of Rosa sericea, which usually has only four. Each petal is divided into two distinct lobes and is usually white or pink, though in a few species yellow or red. Beneath the petals are five sepals (or in the case of some Rosa sericea, four). These may be long enough to be visible when viewed from above and appear as green points alternating with the rounded petals. There are multiple superior ovaries that develop into achenes.[4] Roses are insect-pollinated in nature.*
\end{document}
\end{Verbatim}
\end{document}

But the right side of my text is not align correctly. My line number is not correctly too. And also I have the below warning:

Underfull \hbox (badness 10000) in paragraph

enter image description here

  • Just to make sure, do you have \begin{document} and \end{document} twice on purpose? – AML May 8 '18 at 11:43
  • Do you require use of verbatim, or do you just want it to look a certain way? You might be better off making it look like verbatim without actually using verbatim. – AML May 8 '18 at 17:15
  • @AML No. I require using verbatim. – SH.Madadpour May 8 '18 at 17:24
  • 1
    @AML Thanks a lot for your bounty for my question – SH.Madadpour May 10 '18 at 15:23
  • @SH.Madadpour: Your example doesn't need the use of verbatim or Verbatim. Why do you need it? – Werner May 10 '18 at 16:30
4
+50

based on https://tex.stackexchange.com/a/799/36296

\documentclass{article}
\usepackage{fancyvrb}
%\usepackage{ragged2e}
\usepackage{lipsum}
\usepackage{pbox}

\newcommand*\justify{%
  \fontdimen2\font=0.4em% interword space
  \fontdimen3\font=0.2em% interword stretch
  \fontdimen4\font=0.1em% interword shrink
  \fontdimen7\font=0.1em% extra space
  \hyphenchar\font=`\-% allowing hyphenation
}


\begin{document}
\begin{Verbatim}[numbers=left,fontsize=\bf,commandchars=\^\#\*,baselinestretch=1,firstnumber=1,stepnumber=1]
\begin{document}
^justify^pbox#12cm*#The flowers of most species have five petals, with the exception of Rosa sericea, which usually has only four. Each petal is divided into two distinct lobes and is usually white or pink, though in a few species yellow or red. Beneath the petals are five sepals (or in the case of some Rosa sericea, four). These may be long enough to be visible when viewed from above and appear as green points alternating with the rounded petals. There are multiple superior ovaries that develop into achenes.[4] Roses are insect-pollinated in nature.*
\end{document}
\end{Verbatim}
\end{document}

enter image description here


Maybe a simple alternative to number every line:

\documentclass{article}

\newcommand*\justify{%
  \fontdimen2\font=0.4em% interword space
  \fontdimen3\font=0.2em% interword stretch
  \fontdimen4\font=0.1em% interword shrink
  \fontdimen7\font=0.1em% extra space
  \hyphenchar\font=`\-% allowing hyphenation
}

\usepackage{lineno}

\begin{document}

\begin{linenumbers}
\ttfamily
\setlength{\parindent}{0pt}
\justify

\verb|\begin{document}|

The flowers of most species have five petals, with the exception of Rosa sericea, which usually has only four. Each petal is divided into two distinct lobes and is usually white or pink, though in a few species yellow or red. Beneath the petals are five sepals (or in the case of some Rosa sericea, four). These may be long enough to be visible when viewed from above and appear as green points alternating with the rounded petals. There are multiple superior ovaries that develop into achenes.[4] Roses are insect-pollinated in nature.

\verb|\end{document}|
\end{linenumbers}

\end{document}

enter image description here

  • Thanks a lot . The text was aligned successfully. But the line numbers is not correct. – SH.Madadpour May 10 '18 at 14:43
  • @SH.Madadpour From the point of view of the verbatim environment it is only a single line because it only sees the pbox. If you want automatic line breaking I suggest to use the Listings package. – user36296 May 10 '18 at 14:49
  • @samcarter Would you kindly add a listings version, and then I will award you the bounty? – AML May 10 '18 at 15:02
  • @AML Thanks a lot for the bounty! Instead of the listings version I added one with linenumbers :) – user36296 May 11 '18 at 15:05
  • @samcarter He accepted your answer, so I assumed that solved his issue ¯_(ツ)_/¯ – AML May 11 '18 at 15:08
5

There are three problems: the line number is in the middle of the paragraph, the paragraph is not justified and the line skip between the paragraph and the lines \begin{document} and \end{document} is to small.

The first can be solved with the optional argument [t] to \pbox, which set the baseline of the \pbox to its first line.

The third problem can be solved by adding \strut to the beginning and end of the paragraph.

For the second problem, justifying the paragraph, a little bit of help from this answer (the same as in samcarters answer) is needed. But this sets the fontdimens globally, which may not be wanted. Therefore I wrote macros for storing, setting and restoring them. And to avoid having to type all of this every time, there is also a macro \justifiedtt with the same syntax as \pbox (which is uses internally).

Here are the results of the different parts of the solution (I just shortened the text a little):

enter image description here

And here is the code:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{ragged2e}
\usepackage{lipsum}
\usepackage{pbox}

\makeatletter
\newdimen\@orig@fdiii
\newdimen\@orig@fdiv
\newcount\@orig@hych
\newcommand*\@set@interword{%
  \@orig@fdiii=\fontdimen3\font
  \@orig@fdiv=\fontdimen4\font
  \@orig@hych=\hyphenchar\font
  \fontdimen3\font=0.2em% interword stretch
  \fontdimen4\font=0.1em% interword shrink
  \hyphenchar\font=`\-% allowing hyphenation
}
\newcommand*\@restore@interword{%
  \fontdimen3\font=\@orig@fdiii
  \fontdimen4\font=\@orig@fdiv
  \hyphenchar\font=\@orig@hych
}

\newcommand{\justifiedtt}[3][c]{\pbox[#1]{#2}{\ttfamily\@set@interword\strut#3\strut\@restore@interword}}
\makeatother

\begin{document}
\noindent
Original:
\begin{Verbatim}[numbers=left,fontsize=\bf,commandchars=\^\#\*,baselinestretch=1,firstnumber=1,stepnumber=1]
\begin{document}
^pbox#^textwidth*#The flowers of most species have five petals, with the exception of Rosa sericea, which usually has only four. Each petal is divided into two distinct lobes and is usually white or pink, though in a few species yellow or red.*
\end{document}
\end{Verbatim}

\noindent
With \verb|[t]|:
\begin{Verbatim}[numbers=left,fontsize=\bf,commandchars=\^\#\*,baselinestretch=1,firstnumber=1,stepnumber=1]
\begin{document}
^pbox[t]#^textwidth*#The flowers of most species have five petals, with the exception of Rosa sericea, which usually has only four. Each petal is divided into two distinct lobes and is usually white or pink, though in a few species yellow or red.*
\end{document}
\end{Verbatim}

\noindent
With \verb|\strut| at start and end:
\begin{Verbatim}[numbers=left,fontsize=\bf,commandchars=\^\#\*,baselinestretch=1,firstnumber=1,stepnumber=1]
\begin{document}
^pbox#^textwidth*#^strut#*The flowers of most species have five petals, with the exception of Rosa sericea, which usually has only four. Each petal is divided into two distinct lobes and is usually white or pink, though in a few species yellow or red.^strut*
\end{document}
\end{Verbatim}

\noindent
With \verb|\justifiedtt|:
\begin{Verbatim}[numbers=left,fontsize=\bf,commandchars=\^\#\*,baselinestretch=1,firstnumber=1,stepnumber=1]
\begin{document}
^justifiedtt[t]#^textwidth*#The flowers of most species have five petals, with the exception of Rosa sericea, which usually has only four. Each petal is divided into two distinct lobes and is usually white or pink, though in a few species yellow or red.*
\end{document}
\end{Verbatim}

\noindent
\verb|\justifiedtt| outside \verb|Verbatim| also works:

\noindent
\justifiedtt[t]{\textwidth}{The flowers of most species have five petals, with the exception of Rosa sericea, which usually has only four. Each petal is divided into two distinct lobes and is usually white or pink, though in a few species yellow or red.}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.