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.

9
  • 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
    Commented Jun 6, 2020 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
    Commented Jun 6, 2020 at 23:44
  • @TobiBS because neither what you said works. Commented Jun 6, 2020 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 tex.meta.stackexchange.com/questions/228/…
    – TobiBS
    Commented Jun 7, 2020 at 0:00
  • 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
    Commented Jun 7, 2020 at 7:55

1 Answer 1

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}

Not the answer you're looking for? Browse other questions tagged .