Text Formattingcenter a text in verbatim

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
Tie
Posts: 9
Joined: Tue Dec 22, 2009 4:10 pm

center a text in verbatim

Post by Tie »

Hello all,

I just have one line in verbatim:

\begin{verbatim}
the line
\end{verbatim}

And i would like to center it. It seems that \center, \centerline do not work here. I do not want to use minipage because the length of the line could change.

By the way, the reason why i insist to envelop a line within a verbatim is that i have other similar lines/paragraphs handled by verbatim. I would like to align their font size and face, etc.

Does anyone know the answer?

Thanks and regards

Tie CHENG

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

center a text in verbatim

Post by Stefan Kottwitz »

Hi Tie,

you could use \verb:

Code: Select all

\documentclass[a4paper,10pt]{article}
\begin{document}
\centering
\verb|the line|
\end{document}
Stefan
LaTeX.org admin
Tie
Posts: 9
Joined: Tue Dec 22, 2009 4:10 pm

Re: center a text in verbatim

Post by Tie »

Your solution works very well, thank you very much, Stefan!
kerciyes
Posts: 1
Joined: Thu Apr 01, 2021 1:38 pm

center a text in verbatim

Post by kerciyes »

I've just came across this problem. Why not use spaces to put text in middle in verbatim environment? Verbatim is basically what you see is what you get, isn't it? The proposed solution did not work for me as "\centering" centers everything afterwards. "{\centering \begin{Verbatim} ... } did not have any effect either.
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

center a text in verbatim

Post by Ijon Tichy »

If you change the margins you would have to change the number of spaces. So this wouldn't be a good idea.

Code: Select all

\documentclass[a4paper,10pt]{article}
\usepackage{verbatim}
\makeatletter
\newenvironment{centeredverbatim}{\expandafter\verbatim\centering}{\endverbatim}
\makeatother
\begin{document}
\begin{centeredverbatim}
Bimm
Verbatim
using
automatic \centering
This
Lines
\end{centeredverbatim}
\end{document}
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
Post Reply