0

Is it possible to use \justify (from this previous question) with \VerbatimInput?

\begin{filecontents*}{\jobname.txt}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
\end{filecontents*}
\documentclass{article}
    \usepackage{lipsum}
    \usepackage{fancyvrb}
    \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
    }
    %--------------------------------------------
    % redefine \VerbatimInput
    \RecustomVerbatimCommand{\VerbatimInput}{VerbatimInput}%
    {fontsize=\footnotesize,
     framesep=2em, % separation between frame and text
    }
    \begin{document}
    %\texttt{\justify\lipsum[1]}
\VerbatimInput{\jobname.txt}

\end{document}

  • 1
    verbatim input is designed to give a monospace font with the same layout as the source, what would justification mean in such a context? If you want justified text you presumably don't want verbatim? – David Carlisle Jun 29 '22 at 19:02
  • @DavidCarlisle I'd like to get text justification like in this previous question https://tex.stackexchange.com/questions/430600/how-can-i-justify-text-with-line-number-in-verbatim-environment – Arianna Angeletti Jun 29 '22 at 19:11
  • @AriannaAngeletti Do you really want verbatim or is it just for typewriter type? – egreg Jun 29 '22 at 19:53
  • 1
    Why do you want that? It seems very weird to use a monospace font without using fixed width spaces, and if you use fixed spaces you can not justify. – David Carlisle Jun 29 '22 at 20:06

1 Answers1

2

Justification and verbatim mode don't go along, because typically verbatim environments examine one line at a time and typeset it as a single line paragraph, possibly exceeding the line width.

If you just want to emulate the advanced typewriters that could justify lines, you can more simply declare \ttfamily, adjust the font dimensions and rely on standard justification.

The adjustments should be turned off at the end, so as not to change other applications of \ttfamily.

\begin{filecontents*}{\jobname.txt}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
\end{filecontents*}

\documentclass{article} \usepackage{lipsum} \usepackage{alltt}

\newcommand*\justify{% \edef\restore{% \fontdimen2\font=\the\fontdimen2\font \fontdimen3\font=\the\fontdimen3\font \fontdimen4\font=\the\fontdimen4\font \fontdimen7\font=\the\fontdimen7\font \hyphenchar\font=\the\hyphenchar\font }% \fontdimen2\font=0.5em % interword space \fontdimen3\font=0.22222em % interword stretch \fontdimen4\font=0.11111em % interword shrink \fontdimen7\font=0.11111em % extra space \hyphenchar\font=`- % allowing hyphenation } %-------------------------------------------- \newcommand{\ttinput}[1]{% \par\addvspace{\topsep} \begingroup \setlength{\parindent}{0pt}% \ttfamily \justify \input{#1}% \par \restore \endgroup \addvspace{\topsep} }

\begin{document}

\ttinput{\jobname.txt}

\end{document}

enter image description here

egreg
  • 1,121,712