2

I am relatively new to LaTex and I am trying to add a single landscape table to my document. I have looked at this question and the given \afterpage example works great in my environment. However as soon as I try to insert a listing into my table, I am no longer able to compile the file. Here's the relevant code:

\documentclass[a4paper]{report}

\usepackage{pdflscape}
\usepackage{afterpage}
\usepackage{caption}
\usepackage{listings}
\usepackage{color}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{frame=none,
  language=Java,
  aboveskip=3mm,
  belowskip=0mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=none,
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=3
}

\begin{document}

\afterpage{
     \clearpage
     \thispagestyle{empty}
     \begin{landscape}
         \centering
         \begin{tabular}{llll}
           \hline
           A & B & C &
           \begin{lstlisting}
           public int add(int a, int b){
             int sum = a + b;
             return sum;
           }
           \end{lstlisting}
           \\
           \hline
         \end{tabular}
         \captionof{table}{Table caption}
    \end{landscape}
    \clearpage
}

\end{document}

The error I get when running pdflatex is:

Runaway argument?
! Paragraph ended before \lst@next was complete.
<to be read again>
                   \par
l.103 \newpage
              {\pagestyle{empty} \cleardoublepage}
?

Help would be greatly appreciated!

1 Answers1

4

listings can not be used in the argument of another command (a general restriction of verbatim-like commands) but the command \afterpage here is doing nothing so you can simply delete it:

\documentclass[a4paper]{report}

\usepackage{pdflscape}
\usepackage{afterpage}
\usepackage{caption}
\usepackage{listings}
\lstset{frame=none,
  language=Java,
  aboveskip=3mm,
  belowskip=0mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=none,
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=3
}

\usepackage{xcolor}% missing
\begin{document}


     \thispagestyle{empty}
     \begin{landscape}
         \centering
         \begin{tabular}{llll}
           \hline
           A & B & C &
           \begin{lstlisting}
           public int add(int a, int b){
             int sum = a + b;
             return sum;
           }
           \end{lstlisting}
           \\
           \hline
         \end{tabular}
         \captionof{table}{Table caption}
    \end{landscape}


\end{document}
David Carlisle
  • 757,742
  • you're right this works. However I have some text before and after the table and that's why i need the \afterpage. Any ideas on that? – HolySeven Aug 03 '18 at 19:48
  • @HolySeven I wouldn't use \afterpage except as a last resort (and I wrote it:-) and having any command around listings stops you using listings, You can always just place the landscape environment at the right place manually. – David Carlisle Aug 03 '18 at 20:43
  • @David_Carlisle okay thanks. I am not entirely happy with it, but I guess I have no other choice. – HolySeven Aug 04 '18 at 06:52
  • 1
    @HolySeven oh there are always other choices in a system like tex:-) The above is almost certainly what I'd do (and afterpage, landscape and longtable are all my code) but if you really need listings in the argument of another command use the listings file input form rather than having the code fragment inline in your main document, having it in a file protects the catcode changes and it is far more robust. – David Carlisle Aug 04 '18 at 19:25
  • 1
    @David_Carlisle that did the trick. I was unaware of the \lstinputlisting option. Thanks a lot! – HolySeven Aug 04 '18 at 19:54