0

I'd like to center (horizontally) the lstlisting environment in the page. I tried using "xleftmargin=0.2\textwidth" but the result is not that satisfying. I tried to include the lstlisting env in a center env (parent-child), but it doesn't work. I even tried including lstlisting in a figure env, then \centering and then the lstlising, but this didn't work too.

I looked at several posts, especially this one, but the given solution is not actually working for me. I'll leave the code I wrote down.

\documentclass{article}
\usepackage{listings}
\begin{document}
   \lstset{frame=none,
    language=SQL,
    aboveskip=3mm,
    belowskip=3mm,
    showstringspaces=false,
    columns=flexible,
    basicstyle={\ttfamily},
    numbers=none,
    numberstyle=\tiny\color{gray},
    keywordstyle=\bfseries,
    commentstyle=\color{dkgreen},
    stringstyle=\color{mauve},
    tabsize=4,
    captionpos=b
   }

\begin{lstlisting} create table Student ( Number char(16) primary key, Name varchar(30), Surname varchar(30) ) \end{lstlisting} \end{document}

I also noticed that without specifying any "tabsize" value, it gets centered automatically, but I really need that value for tabsize.

frad
  • 571

1 Answers1

0

The solution from linked question works and I don't see differences with or without tabsize=4. Note the added option gobble.

\documentclass{article}
\usepackage[pass, showframe]{geometry} % show centering effect
\usepackage{listings}
\usepackage{fancybox}

\makeatletter \newenvironment{CenteredBox}{% \begin{Sbox}% }{% Save the content in a box \end{Sbox}\centerline{\parbox{\wd@Sbox}{\TheSbox}}% }% And output it centered \makeatother

\begin{document} \lstset{frame=none, language=SQL, aboveskip=3mm, belowskip=3mm, showstringspaces=false, columns=flexible, basicstyle={\ttfamily}, numbers=none, numberstyle=\tiny\color{gray}, keywordstyle=\bfseries, commentstyle=\color{dkgreen}, stringstyle=\color{mauve}, tabsize=4, captionpos=b }

\begin{CenteredBox} % "gobble" IS required \begin{lstlisting}[gobble=7] create table Student ( Number char(16) primary key, Name varchar(30), Surname varchar(30) ) \end{lstlisting} \end{CenteredBox} \end{document}

enter image description here

muzimuzhi Z
  • 26,474