Search code examples
latexorg-modelistings

Preventing org-mode from translating a comment as an itemize environment


I have an org-mode document which I want to export as a PDF. I'm using the LaTeX listings package to generate nicely formatted code listings, which look like this in org:

#+BEGIN_LaTeX
\begin{lstlisting}[language=Java]
    /** Comment comment comment
     * 
     * blah blah blah
     * 
     * @return comment
     */
    public void foo() {
        return;
    }
\end{lstlisting}
#+END_LaTeX

The Javadoc comment there is being translated by org as a LaTeX itemize environment, like this:

\begin{lstlisting}[language=Java]
    /** Comment comment comment
\begin{itemize}
\item 
\item blah blah blah
\item 
\item @return comment
\end{itemize}
     */
    public void foo() {
        return;
    }
\end{lstlisting}

How can I prevent this from happening and keep the Javadoc as I originally wrote it? If I use #+BEGIN_SRC rather than #+BEGIN_LaTeX what I get back is a verbatim environment, but I want to stick with listings rather than verbatim or minted since I've already made the effort to put together a nice set of stylings for it.


Solution

  • What you are ultimately wanting is a literal example. Essentially you want the code to be exported, but fontified. You need to tell org-mode to use listings (or minted) upon export. This can be done in your .emacs file:

    ;; tell org to use listings with colors                                                     
    (setq org-export-latex-listings t)
    (add-to-list 'org-export-latex-packages-alist '("" "listings"))
    (add-to-list 'org-export-latex-packages-alist '("" "color"))
    

    Also, with this you don't need to specify the listings package in a header argument to your document. Now, source code blocks will be exported in the appropriate lstlistings environment:

    #+begin_src java                                                                
      /** Comment comment comment                                                   
       *                                                                            
       * blah blah blah                                                             
       * @return comment                                                            
       */                                                                           
      public void foo() {                                                           
        return;                                                                   
      }                                                                             
    #+end_src
    

    gets exported to LaTeX as

    \lstset{language=java}
    \begin{lstlisting}
    /** Comment comment comment
     *
     * blah blah blah
     * @return comment
     */
    public void foo() {
        return;
    }
    \end{lstlisting}
    

    I'm not quite sure why, when you use the #+begin_latex...#+end_latex block in your example, that things are getting parsed weirdly. In principle, one would like whatever is in the LaTeX block to be passed as-is to the .tex file.