Discussion:
centering a verbatim
(too old to reply)
Nikola Skoric
2007-10-08 18:36:25 UTC
Permalink
Is there a way to center a verbatim object? I have a bunch of text
that I wanted in verbatim object, but it is now justified to left, and
I'd like it to be in the center of the page. Now, I know, "verbatim"
means "no messing with justification, just displaying what is typed
in", but could the whole object be centered? The way you do it with a
picture. Picture is put in the center of the page, it is not aligned
on left margin... I hope I make sence.
--
"Now the storm has passed over me
I'm left to drift on a dead calm sea
And watch her forever through the cracks in the beams
Nailed across the doorways of the bedrooms of my dreams"
Joost Kremers
2007-10-08 18:43:22 UTC
Permalink
Post by Nikola Skoric
Is there a way to center a verbatim object? I have a bunch of text
that I wanted in verbatim object, but it is now justified to left, and
I'd like it to be in the center of the page. Now, I know, "verbatim"
means "no messing with justification, just displaying what is typed
in", but could the whole object be centered? The way you do it with a
picture. Picture is put in the center of the page, it is not aligned
on left margin... I hope I make sence.
first thing i'd try is to wrap it in a minipage environment that's not as
wide as the text, and put that in a center environment.
--
Joost Kremers ***@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)
Nikola Skoric
2007-10-08 18:57:40 UTC
Permalink
Dana 8 Oct 2007 18:43:22 GMT,
Post by Joost Kremers
Post by Nikola Skoric
Is there a way to center a verbatim object? I have a bunch of text
that I wanted in verbatim object, but it is now justified to left, and
I'd like it to be in the center of the page. Now, I know, "verbatim"
means "no messing with justification, just displaying what is typed
in", but could the whole object be centered? The way you do it with a
picture. Picture is put in the center of the page, it is not aligned
on left margin... I hope I make sence.
first thing i'd try is to wrap it in a minipage environment that's not as
wide as the text, and put that in a center environment.
Oh, yes, minipage, now google returns useful results... Thanks.

Hm, minipage takes an argument which is (if I understood correctly) width
of that minipage. Now, it seems that I have to guess the width of my text
because \begin{center} works properly only when I set that minipage
argument to 22mm. Is there any other way to do this than manual calibration?

Here is my example:

\begin{table}[h]
\begin{center}
\begin{minipage}{22mm}
\begin{verbatim}
00000000000000
FFFFFFFFFFFFFF
0000000FFFFFFF
FFFFFFF0000000
\end{verbatim}
\end{minipage}
\caption{Slabi kljucevi}
\label{tab:slabi_kljucevi}
\end{center}
\end{table}
--
"Now the storm has passed over me
I'm left to drift on a dead calm sea
And watch her forever through the cracks in the beams
Nailed across the doorways of the bedrooms of my dreams"
Joost Kremers
2007-10-08 19:28:45 UTC
Permalink
Post by Nikola Skoric
Hm, minipage takes an argument which is (if I understood correctly) width
of that minipage.
yes. a minipage is in actual fact a box, and the argument specifies the
width of that box.
Post by Nikola Skoric
Now, it seems that I have to guess the width of my text
because \begin{center} works properly only when I set that minipage
argument to 22mm.
yes, that makes sense. if you specify a width smaller than the text, LaTeX
will happily print the part of the text that is too wide outside the
box. what is centered is the box, but because part of the text falls
outside of it, the text itself is not centered. if the width of the box is
wider than the text, you get a similar effect: the box is centered, but the
actual width of the box is wider than the text, so that the text doesn't
look centered.
Post by Nikola Skoric
Is there any other way to do this than manual calibration?
i think so: create a new length command and set it to the width of the
widest line in your verbatim environment:

========================

\documentclass{article}

\newlength{\myverbatimwidth}

\begin{document}

\settowidth{\myverbatimwidth}{\tt 00000000000000}

\begin{table}[h]
\begin{center}
\begin{minipage}{\mytextwidth}
\begin{verbatim}
00000000000000
FFFFFFFFFFFFFF
0000000FFFFFFF
FFFFFFF0000000
\end{verbatim}
\end{minipage}
\caption{Slabi kljucevi}
\label{tab:slabi_kljucevi}
\end{center}
\end{table}

\end{document}

========================
--
Joost Kremers ***@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)
Nikola Skoric
2007-10-08 20:02:25 UTC
Permalink
Dana 8 Oct 2007 19:28:45 GMT,
Post by Joost Kremers
Post by Nikola Skoric
Is there any other way to do this than manual calibration?
i think so: create a new length command and set it to the width of the
Oh, man, latex just keeps on surprising me :-) This thing can do
anything... Anyway, I'm a newbie so I didn't know what \tt is. Now that I
found out what it does, it turns out I can get what I want in a much simpler
way:

\begin{table}[h]
\begin{center}
\begin{tt}
00000000000000 \\
FFFFFFFFFFFFFF \\
0000000FFFFFFF \\
FFFFFFF0000000 \\
\end{tt}
\caption{Slabi kljucevi}
\label{tab:slabi_kljucevi}
\end{center}
\end{table}

But, thanks anyway, this verbratim trick will be very usefull when I start
pasting bits of code into my paper...
--
"Now the storm has passed over me
I'm left to drift on a dead calm sea
And watch her forever through the cracks in the beams
Nailed across the doorways of the bedrooms of my dreams"
Joost Kremers
2007-10-08 20:50:18 UTC
Permalink
Post by Nikola Skoric
Oh, man, latex just keeps on surprising me :-) This thing can do
anything...
oh yeah. ;-) TeX is a programming language, turing-complete and
all. perhaps not the most elegant one in existence, but still.
Post by Nikola Skoric
Anyway, I'm a newbie so I didn't know what \tt is. Now that I
found out what it does, it turns out I can get what I want in a much simpler
yup, that'll work too...
Post by Nikola Skoric
But, thanks anyway, this verbratim trick will be very usefull when I start
pasting bits of code into my paper...
if you're going to typeset code, then you should also check out the
listings package. may be useful.
--
Joost Kremers ***@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)
Enrico Gregorio
2007-10-08 21:17:24 UTC
Permalink
Post by Nikola Skoric
Dana 8 Oct 2007 19:28:45 GMT,
Post by Joost Kremers
Post by Nikola Skoric
Is there any other way to do this than manual calibration?
i think so: create a new length command and set it to the width of the
Oh, man, latex just keeps on surprising me :-) This thing can do
anything... Anyway, I'm a newbie so I didn't know what \tt is. Now that I
found out what it does, it turns out I can get what I want in a much simpler
\begin{table}[h]
\begin{center}
\begin{tt}
00000000000000 \\
FFFFFFFFFFFFFF \\
0000000FFFFFFF \\
FFFFFFF0000000 \\
\end{tt}
\caption{Slabi kljucevi}
\label{tab:slabi_kljucevi}
\end{center}
\end{table}
But, thanks anyway, this verbratim trick will be very usefull when I start
pasting bits of code into my paper...
\begin{table}[ht] % leave LaTeX a chance
\centering\ttfamily
00000000000000 \\
FFFFFFFFFFFFFF \\
0000000FFFFFFF \\
FFFFFFF0000000 \\
\caption{Slabi kljucevi}
\label{tab:slabi_kljucevi}
\end{table}

Of course this works only if all lines have the same length. But
you can get a good result also for unequal lines with a one column
alignment:

\begin{table}[ht]
\centering
{\ttfamily
\begin{tabular}{l}
00000000000000 \\
FFFFFFFFFFFFFF \\
0000000FFFFFFF \\
FFFFFFF0000000
\end{tabular}}
\caption{Slabi kljucevi}
\label{tab:slabi_kljucevi}
\end{table}

Note that \tt is an obsolete command; the preferred declaration
is \ttfamily, which you can also use as an environment. But a pair
of braces is enough.

Even better could be to load the array package and write

\begin{table}[ht]
\centering
\begin{tabular}{>{\ttfamily}l}
00000000000000 \\
FFFFFFFFFFFFFF \\
0000000FFFFFFF \\
FFFFFFF0000000
\end{tabular}
\caption{Slabi kljucevi}
\label{tab:slabi_kljucevi}
\end{table}

because in this way you can also add other columns to the alignment;
they will be in normal style or the one you prefer.

Ciao
Enrico
Ulrike Fischer
2007-10-09 07:10:10 UTC
Permalink
Post by Nikola Skoric
Post by Joost Kremers
Post by Nikola Skoric
Is there a way to center a verbatim object?
first thing i'd try is to wrap it in a minipage environment that's not as
wide as the text, and put that in a center environment.
center add some vertical spaces, in table \centering is better.
Post by Nikola Skoric
Oh, yes, minipage, now google returns useful results... Thanks.
Hm, minipage takes an argument which is (if I understood correctly) width
of that minipage. Now, it seems that I have to guess the width of my text
because \begin{center} works properly only when I set that minipage
argument to 22mm. Is there any other way to do this than manual calibration?
\documentclass{article}

\usepackage{varwidth}

\begin{document}

\begin{table}[h]
\noindent x\hfill x%for show

\centering

\begin{varwidth}{\linewidth}
\begin{verbatim}
00000000000000
FFFFFFFFFFFFFF
0000000FFFFFFF
FFFFFFF0000000
\end{verbatim}
\end{varwidth}

\caption{Slabi kljucevi}
\label{tab:slabi_kljucevi}

\end{table}

\end{document}
--
Ulrike Fischer
Continue reading on narkive:
Loading...