Latex: center text in listing
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?
a source to share
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).
a source to share