1

Here is the code

\lstdefinestyle{mystyle}{
    frame=single,
}

\begin{figure*}[tbp!]
    \begin{center}
        \begin{tabular}{c}
            \lstset{style=mystyle}
            \lstinputlisting[]{code/method.cs}
        \end{tabular}       
    \end{center}
\end{figure*}

what's defined in method.cs:

public string[] reverse(string[] array)
{
    string[] newArray = new string[array.Length];
    for (int index = 0; index < array.Length; index++)
        newArray[array.Length-index-1] = array[index];

    return newArray;
}

Error message:

Missing { inserted. \lstset{style=mystyle}
Missing } inserted. \lstset{style=mystyle}
Extra alignment tab has been changed to \cr. \lstset{style=mystyle}
You can't use `macro parameter character #' in restricted horizontal mode. \lstset{style=mystyle}
You can't use `macro parameter character #' in restricted horizontal mode. \lstset{style=mystyle}
You can't use `macro parameter character #' in restricted horizontal mode. \lstset{style=mystyle}
....

It seems removing the tabular environment works but I need that for centering purposes. What's the error?

Edit:

complete MWE:

\documentclass{article}

\usepackage{listings}

\lstdefinestyle{mystyle}{
    % backgroundcolor=\color{backcolour},   
    frame=single,
}

\begin{document}

\begin{figure*}[tbp!]
    \begin{center}
        %\begin{tabular}{c}
            \lstset{style=mystyle}
            \lstinputlisting[]{method.cs}
        %\end{tabular}      
    \end{center}
\end{figure*}

\end{document}

Again, I want the tabular environment. In any case, I'd like to know why this does not work. So please don't suggest to delete the tabular which I already knew would work.

user1935724
  • 1,339
  • What do you mean by "I need the tabular environment for centering purposes"? What is wrong with the center environment, or instead a simple \centering in the figure? – TobiBS Jun 06 '20 at 23:39
  • Also, can you please post a complete MWE? Just adding \usepackage{listings} and using class article doesn't work at my side at all. – TobiBS Jun 06 '20 at 23:44
  • @TobiBS because neither what you said works. – user1935724 Jun 06 '20 at 23:55
  • Please read my texts again, I didn't propose solutions, I am trying to get an idea of your problem. For this it would be helpful to get a MWE, please see https://tex.meta.stackexchange.com/questions/228/ive-just-been-asked-to-write-a-minimal-working-example-mwe-what-is-that – TobiBS Jun 07 '20 at 00:00
  • @TobiBS I am answering your question "What is wrong with the center environment, or instead a simple \centering in the figure?" You asked that right? – user1935724 Jun 07 '20 at 00:01
  • @TobiBS Are you telling me you don't even know what you are asking yourself. B.T.W, keep downvoting the question, I am so frustrated man! – user1935724 Jun 07 '20 at 00:09
  • I assume you refer to this answer when using \begin{tabular}{c} in order to horizontally center a listing. – leandriis Jun 07 '20 at 07:54
  • 1
    On my system, I can't reproduce the error messages you included in your question. For me, the box is already centered on the page even without figure*, center and tabular, although it is a bit wider than the actual textwidth itself. (You can add \usepackage{showframe} to your preamble to get a visual representation of the width of the textwidth) – leandriis Jun 07 '20 at 07:55
  • @leandriis I now understand the need for tabular. I updated my answer accordingly. The frame definition of lstlistings makes use of & character and that confuses the tabular environment. So much that even if you use frame=none you'd end up with errors... – Elad Den Jun 07 '20 at 14:53

1 Answers1

3

Using your MWE the listings environment is already centered, the problem is that its width is too long and therefore it looks like the text is on the left. A solution for this is this answer (as @leandriis pointed out).

The thing that causes the error in your case is the frame. You cannot have a frame in a tabular environment. This is because the definition of frame does not play nice with tabular. You should use the tables separators with \begin{tabular}{|c|} and \hline to create a frame.

MWE of both options with the boundaries shown with geometry:

\documentclass{article}
\usepackage[showframe]{geometry}

\usepackage{listings}

\lstdefinestyle{mystyle}{
    %backgroundcolor=\color{green},   
    frame=single,
}

\begin{document}

\begin{figure*}[tbp!]
    \begin{center}
        \begin{tabular}{|c|}
            \hline
            \lstinputlisting[]{method.cs}\\
            \hline
        \end{tabular}      
    \end{center}
\end{figure*}

\begin{figure*}[tbp!]
    \begin{center}
        %\begin{tabular}{|c|}
            \lstset{style=mystyle}
            \lstinputlisting[]{method.cs}
        %\end{tabular}      
    \end{center}
\end{figure*}

\end{document}
Elad Den
  • 3,941