Centering verbatim and alltt

17 views
Skip to first unread message

Jeff Barnett

unread,
Dec 2, 2021, 2:04:51 AM12/2/21
to
I'm using an older versions of WinEdt with MiKTeX V2.9 and PDFLaTeX
3.14.....-2.6-1.45.15. I'm preparing a document that has some verbatim
and alltt environments in figures. Normally, I just slip a \centering in
after the \begin{figure} but that doesn't work. I've tried a \hspace,
\hspace*, \hskip, and \rule to push the typeset stuff right but they
don't work either. The obvious question: Is there some relatively simple
way to do this?

Thanks in advance for any thoughts or suggestions.
--
Jeff Barnett

Peter Flynn

unread,
Dec 4, 2021, 6:04:28 PM12/4/21
to
On 02/12/2021 07:04, Jeff Barnett wrote:
> I'm using an older versions of WinEdt with MiKTeX V2.9 and PDFLaTeX
> 3.14.....-2.6-1.45.15. I'm preparing a document that has some verbatim
> and alltt environments in figures. Normally, I just slip a \centering in
> after the \begin{figure} but that doesn't work.

Did you mean you have done this before and it worked, but now it has failed?

> I've tried a \hspace,
> \hspace*, \hskip, and \rule to push the typeset stuff right but they
> don't work either.

They only work for material that is for L-R setting. verbatim and alltt
are blocks which occur in vertical mode, so they can't be pushed right
that way (actually they can, but it has other side-effects).

> The obvious question: Is there some relatively simple
> way to do this?

These environments use the surrounding value of \textwidth as their
width, so you have to use a mechanism which changes that.

One way would be to use the minipage environment (I have added material
here to indicate widths and where the page margins fall):

========================================================================
\documentclass{article}
\begin{document}
\noindent
Top right
\hfill
Top left
\par
\section{Heading}
Some text

\begin{figure}[hb]
\caption{Example}
\centering
\begin{minipage}{.75\textwidth}
\hrule
\par\medskip
\begin{verbatim}
%{19}
\NewLipsumPar{Nisi mihi Phaedrum, inquam,
tu mentitum aut Zenonem putas, quorum
utrumque audivi, cum mihi nihil sane
praeter sedulitatem probarent, omnes
mihi Epicuri sententiae satis notae sunt.
Atque eos, quos nominavi, cum Attico
nostro frequenter audivi, cum miraretur
ille quidem utrumque, Phaedrum autem etiam
amaret, cotidieque inter nos ea, quae
audiebamus, conferebamus, neque erat
umquam controversia, quid ego intellegerem,
sed quid probarem.}
\end{verbatim}
\end{minipage}
\end{figure}
\section{Another}
More text
\end{document}
========================================================================

Another is to use a tabular environment of one cell, because this
environment is set in horizontal mode (a tabular environment is
essentially just a large character as far as the line and page
construction mechanism is concerned):

========================================================================
\documentclass{article}
\usepackage{array}
\begin{document}
\noindent
Top right
\hfill
Top left
\par
\section{Heading}
Some text

\begin{figure}[hb]
\caption{Example}
\centering
\begin{tabular}{p{.75\textwidth}}
\hrule
\par\medskip
\begin{verbatim}
%{19}
\NewLipsumPar{Nisi mihi Phaedrum, inquam,
tu mentitum aut Zenonem putas, quorum
utrumque audivi, cum mihi nihil sane
praeter sedulitatem probarent, omnes
mihi Epicuri sententiae satis notae sunt.
Atque eos, quos nominavi, cum Attico
nostro frequenter audivi, cum miraretur
ille quidem utrumque, Phaedrum autem etiam
amaret, cotidieque inter nos ea, quae
audiebamus, conferebamus, neque erat
umquam controversia, quid ego intellegerem,
sed quid probarem.}
\end{verbatim}
\end{tabular}
\end{figure}
\section{Another}
More text
\end{document}
========================================================================

Peter

Jeff Barnett

unread,
Dec 4, 2021, 7:18:08 PM12/4/21
to
On 12/4/2021 4:04 PM, Peter Flynn wrote:
> On 02/12/2021 07:04, Jeff Barnett wrote:
>> I'm using an older versions of WinEdt with MiKTeX V2.9 and PDFLaTeX
>> 3.14.....-2.6-1.45.15. I'm preparing a document that has some verbatim
>> and alltt environments in figures. Normally, I just slip a \centering in
>> after the \begin{figure} but that doesn't work.
>
> Did you mean you have done this before and it worked, but now it has failed?
>
>> I've tried a \hspace,
>> \hspace*, \hskip, and \rule to push the typeset stuff right but they
>> don't work either.
>
> They only work for material that is for L-R setting. verbatim and alltt
> are blocks which occur in vertical mode, so they can't be pushed right
> that way (actually they can, but it has other side-effects).
>
>> The obvious question: Is there some relatively simple
>> way to do this?
>
> These environments use the surrounding value of \textwidth as their
> width, so you have to use a mechanism which changes that.
>
> One way would be to use the minipage environment (I have added material
> here to indicate widths and where the page margins fall):

Thank you for replying. No it hadn't worked before and I couldn't
remember what to do. It's been several years since I've written anything
that needed some sophisticated knowledge of all this stuff. I finally
remembered that, like you suggest, that minipage, etc, could help. So I
used it and remembered why it is a "hack" if I wanted to keep it simple.
The small problem is determining the actual width of the material. What
I did was try widths that were too small and too large than binary
search using the presence or not of a box overflow message to say
whether to increase or decrease the current width estimate. After a few
tries I had narrowed it down to about 1/8"; and as they say, that's good
enough for government work. (Since I'm retired and drawing Social
Security, I now consider my hobbies government work.) I was hoping that
there was some form that did these sizing and fitting things without a
deep dive into the bowels of TeX. When nobody responded for a few days
to such a straightforward question, I was pretty sure the answer was no.

Once again, thank you for responding.

<snip>
--
Jeff Barnett

Peter Flynn

unread,
Dec 5, 2021, 3:41:53 PM12/5/21
to
On 05/12/2021 00:17, Jeff Barnett wrote:
[...]
> Thank you for replying. No it hadn't worked before and I couldn't
> remember what to do. It's been several years since I've written
> anything that needed some sophisticated knowledge of all this stuff.
> I finally remembered that, like you suggest, that minipage, etc,
> could help. So I used it and remembered why it is a "hack" if I
> wanted to keep it simple. The small problem is determining the actual
> width of the material.

You *could* use a tabular environment with a single "l" cell in the
monospace font, and put each line of the code in as a separate row (ie
terminated by \\ — that way tabular will use just as much space as
needed, eg

\usepackage{array}
...
\begin{tabular}{>{\ttfamily}l}
first line\\
much longer second line\\
etc\\
\end{tabular}

> What I did was try widths that were too small and too large than
> binary search using the presence or not of a box overflow message to
> say whether to increase or decrease the current width estimate. After
> a few tries I had narrowed it down to about 1/8"; and as they say,
> that's good enough for government work. (Since I'm retired and
> drawing Social Security, I now consider my hobbies government work.)

:-)

> I was hoping that there was some form that did these sizing and
> fitting things without a deep dive into the bowels of TeX. When
> nobody responded for a few days to such a straightforward question, I
> was pretty sure the answer was no.

No, unlike XML processors, [La]TeX has no read-ahead mechanism to
precalculate a width. It would be possible to write something to do
that, though: essentially an environment that would set its content in a
\hbox and measure its width.

Peter

Ulrike Fischer

unread,
Dec 6, 2021, 3:32:15 AM12/6/21
to
Am Sat, 4 Dec 2021 17:17:57 -0700 schrieb Jeff Barnett:

> I was hoping that there was some form that did these sizing and
> fitting things without a deep dive into the bowels of TeX. When
> nobody responded for a few days to such a straightforward
> question, I was pretty sure the answer was no.


Well there isn't much traffic on c.t.t. nowadays.

And you didn't get the straightforward answer ("use varwidth") from
me, as when I saw the question I only thought "again someone without
a minimal example to use for testing" and then I did go away to do
my own stuff.


--
Ulrike Fischer
http://www.troubleshooting-tex.de/

Alain Ketterlin

unread,
Dec 6, 2021, 3:51:17 AM12/6/21
to
Ulrike Fischer <ne...@nililand.de> writes:

> Am Sat, 4 Dec 2021 17:17:57 -0700 schrieb Jeff Barnett:
>
>> I was hoping that there was some form that did these sizing and
>> fitting things without a deep dive into the bowels of TeX. When
>> nobody responded for a few days to such a straightforward
>> question, I was pretty sure the answer was no.

[...]
> And you didn't get the straightforward answer ("use varwidth") [...]

Another way is to use fancyvrb, which has a {BVerbatim} environment
producing a box, with baseline adjustment and a nice interface for
enabling alltt-like espace/begin/end-group characters (\ { } or
something else if those occur often in the contents of the environment).

-- Alain.

Jeff Barnett

unread,
Dec 6, 2021, 12:50:33 PM12/6/21
to
I thought that my question was so straightforward that virtually
everyone who scans this group would understand it. If it had been any
more complicated, I certainly would have include a minimal example. In
any event, thanks for the tip. varwidth works just fine.
--
Jeff Barnett

Ulrike Fischer

unread,
Dec 6, 2021, 2:50:55 PM12/6/21
to
Am Mon, 6 Dec 2021 10:50:25 -0700 schrieb Jeff Barnett:

>> And you didn't get the straightforward answer ("use varwidth") from
>> me, as when I saw the question I only thought "again someone without
>> a minimal example to use for testing" and then I did go away to do
>> my own stuff.

> I thought that my question was so straightforward that virtually
> everyone who scans this group would understand it.

Understanding the question is not the point. What I access (beside
other things) when looking at a question is the time it would take
me to test the solution I have in mind.

If I have to write the test document myself my willigness to bother
with the question drops by many points.


--
Ulrike Fischer
https://www.troubleshooting-tex.de/

Jeff Barnett

unread,
Dec 6, 2021, 11:43:20 PM12/6/21
to
Well my guess was that you and many others who scan this forum would
know instantaneously what I was asking about and whether there was a
simple approach to solve it. I believe from what you said above that my
guess was exactly correct - you instantly recognized the problem and
know simple ways to circumvent it. However, since you value your time,
you thought it important to insist on a form of question composition so
you could "go quickly" in more complicated cases. I don't think that is
necessarily a bad strategy. I thought there would be some number of
quick responses such as yours that would take the form "look at
varwidth". The only complication in my original message was the spec
that I was using an older OS and TeX setup. I couldn't imagine anyone
running a test case on those old versions. I also couldn't imagine that
anyone who could answer my question would bother to or need to run a
test case.

In any event I appreciate the fact that you did take a moment for me.
--
Jeff Barnett
Reply all
Reply to author
Forward
0 new messages