Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
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. –  Jubobs Jul 27 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. –  Paul Gessler Jul 27 at 22:30

2 Answers 2

up vote 5 down vote accepted

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

share|improve this answer
    
Note that there is a semantic problem, though: "default (no qualifier specified)" is not code. –  Jubobs Jul 28 at 8:43

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.