Latex: center text in listing

How do I place text inside a listing in Latex?

Or in another way that doesn't use listing. (Need a monospaced box)

+2


a source to share


2 answers


Reply to tex.stackexchange :

\documentclass{article}
\usepackage{listings}
\renewcommand{\figurename}{Listing}
                    % replace figurename with the text that should preceed the caption
\begin{document}

\begin{figure}[thp] % the figure provides the caption
\centering          % which should be centered
\caption{Ausgabe des C-Programms}
\begin{tabular}{c}  % the tabular makes the listing as small as possible and centers it
\begin{lstlisting}[label={gtt_c_ausgabe}]
printf("Your code goes here.\n");
\end{lstlisting}
\end{tabular}
\end{figure}

\end{document}

      

... which still surprises me:



Using a framework around the code eg. using \lstset{frame=single,frameround=tttt}

, moves the frame to the right. How can this be avoided?

What does the beat renewcommand

do?

+1


a source


I don't have an answer for the package listing on my head, but you can try this:

\framebox[.9\linewidth]{\parbox{.85\linewidth}{\tt Hello World\\Second line}}

      

This creates a box with 90% line width with text 85% of the line width.



If you want this to be focused, you just put \centering

before the command \tt

:

\framebox[.9\linewidth]{\parbox{.85\linewidth}{\centering \tt Hello World\\Second line}}

      

If you prefer a box without a border, just change \framebox

to \makebox

(and keep the arguments when they are).

0


a source







All Articles