1

I want to create a simple table in this case with the first column having snippet of code, and the secon column describing it.

The command \java is just defined as:

\newcommand{\java}{\begin{lstlisting}[language=Java]}

The whole row becomes the style of my java code.

\begin{tabular}{l|l}
\java
private
\end{lstlisting}
&   Only entities declared inside the class are allowed to refer to private symbols \\

\java
protected
\end{lstlisting}
&   Entities declared inside the class or children of the class \\

\java
public
\end{lstlisting}
&   Everyone is allowed to refer to public symbols          \\

default (no qualifier specified)
&   Entities in the class, children of the class, or anyone in the same package are allowed access.
\end{tabular}
2
  • Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.
    – jub0bs
    Commented Jul 27, 2014 at 22:20
  • 2
    Seems to work fine for me. But see Jubob's suggestions below. Also, personally I think it's a bad practice to encapsulate environments in macros, especially if that encapsulation doesn't include both the beginning and end of the environment. Commented Jul 27, 2014 at 22:30

2 Answers 2

5

Another way to do this is to use the collcell package and define a new columntype for your Java code. As Jubobs points out in the comments, if a cell contains non-code the formatting will be incorrect. You will need to override its specification using \multicolumn. If you have a mix of code and non-code in the same column then a solution like his is clearly better.

\documentclass{article}
\usepackage{array}
\usepackage{listings}
\usepackage{collcell}
\newcommand\java[1]{{\lstinline[language=java]{#1}}}
\newcolumntype{J}{>{\collectcell\java}l<{\endcollectcell}}

\begin{document}

\begin{tabular}{J|p{2in}} 
private
&   Only entities declared inside the class are allowed to refer to private symbols \\
protected
&   Entities declared inside the class or children of the class \\
public
&   Everyone is allowed to refer to public symbols          \\
\multicolumn{1}{l|}{default (no qualifier specified)}
&   Entities in the class, children of the class, or anyone in the same package are allowed access.
\end{tabular}
\end{document}

enter image description here

1
  • Note that there is a semantic problem, though: "default (no qualifier specified)" is not code.
    – jub0bs
    Commented Jul 28, 2014 at 8:43
1

Suggestions: use inline code instead of lstlisting environments, and, for convenience, use a one-character shorthand.

enter image description here

\documentclass{article}

\usepackage{listings}
\lstMakeShortInline[language=Java,basicstyle=\ttfamily]`

\begin{document}

\begin{tabular}{l|p{7cm}}
`private`
  &   Only entities declared inside the class are allowed to refer to private symbols \\
`protected`
  &   Entities declared inside the class or children of the class \\
`public`
  &   Everyone is allowed to refer to public symbols          \\
default (no qualifier specified)
  &   Entities in the class, children of the class, or anyone in the same package are allowed access.
\end{tabular}

\end{document}

You must log in to answer this question.

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