2

I wish to center a code listing with some latex math symbols in there (such as \forall). Unfortunately it causes my listing to get shifted towards the right. It now centers the left edge of the listing instead of the center of the listing.

\begin{figure}[h]
    \centering
    \begin{tabular}{c}{
            \begin{lstlisting}[language=customLanguage]
{
    %*$\forall formula here $*)
    %*$\forall second formula here $*) 
}\end{lstlisting}
    }\end{tabular}
    \caption{my caption.}
    \label{fig:example}
\end{figure}

This is the result: Shifted to the right

However! If I remove the \escapeinside syntax to run Latex code, like so:

\begin{figure}[h]
    \centering
    \begin{tabular}{c}{
            \begin{lstlisting}[language=customLanguage]
{
    \forall formula here $*)
    %*$\forall second formula here $*) 
}\end{lstlisting}
    }\end{tabular}
    \caption{my caption.}
    \label{fig:example}
\end{figure}

Then I get the correct positioning, but obviously not the intended text: enter image description here

Any way I can have my cake and eat it too?

My .sty file:

% Define IDP
\lstdefinelanguage{customLanguage}{
    ... % irrelevant stuff
}
\lstset{
    language=customLanguage,
    tabsize=3,
    basicstyle=\footnotesize,  
    %frame=none,
    %frame=single,
    escapeinside={\%*}{*)},          % if you want to add LaTeX within your code
    showstringspaces=false,
    breaklines = true,
    alsoletter=(,
    commentstyle=\commentstyle,
    keywordstyle=[1],
    keywordstyle=[2]\color{BrickRed}\bfseries,
    keywordstyle=[3]\color{OliveGreen}\bfseries,
    keywordstyle=[4]\color{Blue}\bfseries,
    keywordstyle=[5]\color{Violet}\bfseries,
    literate={~} {$\sim$}{1}
}
\newcommand{\code}[1]{\texttt{#1}}
  • Please, make the code snippets into a full document. – egreg Dec 10 '16 at 22:48
  • 1
    putting a listing with more than one line in a c-type tabular cell is quite daring .. – Ulrike Fischer Dec 10 '16 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.