0

Building on this example:

\documentclass{article}
\RequirePackage{longtable,nameref}

\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{listings}
\lstset{
    frame=single,
    breaklines=true,
    postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\color{red}\hookrightarrow\space}}
}

\lstdefinelanguage{numpy}{
    keywords = {
        and,del,from,not,while,
        as,elif,global,or,with,
        assert,else,if,pass,yield,
        break,except,import,print,
        class,exec,in, raise,continue,
        finally,is, return,def,for,
        lambda,try
    },
    morekeywords = {numpy,fft,mean}
}

\begin{document}

    \begin{longtable}[t]{l l} 
        \caption{Table caption.}
        \label{tab:table1}

        \\

        a &

        \begin{lstlisting}[language=numpy]
        def autocorrelate(A):
        avgsq = A.mean()**2
        sigma = (A**2).mean() - avgsq
        corr = numpy.real(numpy.fft.ifft( numpy.fft.fft(A).conj() * numpy.fft.fft(A) )) / A.shape[0]
        corr = (corr - avgsq)/sigma
        return corr
        \end{lstlisting}
        \\

        b &

        def autocorrelate(A):
        avgsq = A.mean()**2
        sigma = (A**2).mean() - avgsq
        corr = numpy.real(numpy.fft.ifft( numpy.fft.fft(A).conj() * numpy.fft.fft(A) )) / A.shape[0]
        corr = (corr - avgsq)/sigma
        return corr

        \\
    \end{longtable}         

\end{document}

How can I define a new column type in which the entire content of every cell is surrounded by a lstlisting environment without typing it?

  • mmm, do you mean it might work on other environments - like \begin{LARGE}some text \end{LARGE}? – Orion Jul 11 '16 at 11:43
  • LARGE is no environment ;-) – user31729 Jul 11 '16 at 11:45
  • 1
    it is much harder to do this for a verbatim environment such as listings as, by design, verbatim environments disable the normal meaning of & , \\ and \end{longtable} so detecting the end of the cell is tricky. It's not impossible but it's sufficiently fragile and specific to (in this case) listings-inside-longtable, that I wouldn't suggest doing it. – David Carlisle Jul 11 '16 at 12:09
  • @ChristianHupfer well actually, it is.... – David Carlisle Jul 11 '16 at 12:10
  • @DavidCarlisle: You know what I mean... I know that it's possible to use \begin{LARGE}...\end{LARGE} – user31729 Jul 11 '16 at 12:12

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.