7

Using LaTeX, I need to show some code snippet inside a table. Here is an example of what I'm trying to do:

\begin{document}
Par exemple :
\begin{center}
\begin{tabular}{lp{5cm}l}
\hline
Méthode & Description & Exemple d'utilisation\\
\hline
\texttt{isLetter()}& Indique si le caractère est une lettre de l'alphabet. &
\begin{lstlisting}[numbersep=0pt]
QChar MyChar('x');
bool IsLetter = MyChar.isLetter();
\end{lstlisting} \\
\hline
\texttt{toUpper()}& Retourne le même caractère mais en majuscules. & toto \\
\hline
\end{tabular}
\end{center}
\end{document}

Here is the result I get :

.

As you can see, there is a margin on the left of the code. I guess this margin is there for numbering, but I don't need numbering and would like to get rid of it. I've tried changing some options (numbersep, xleftmargin) but none is working as I wish.

UPDATE

Here is the full document demonstrating the problem :

\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern,textcomp}
\usepackage[frenchb]{babel}
\usepackage{listings}

\begin{document}
   \begin{enumerate}
   \item Par exemple :
      \begin{center}
      \begin{tabular}{lp{5cm}l}
      \hline
      Méthode & Description & Exemple d'utilisation\\
      \hline
      \texttt{isLetter()}& Indique si le caractère est une lettre de l'alphabet. &
      \begin{lstlisting}[numbersep=0pt]
QChar MyChar('x');
bool IsLetter = MyChar.isLetter();
// IsLetter vaut vrai
QChar MyChar2('&');
IsLetter = MyChar2.isLetter();
// IsLetter vaut faux
      \end{lstlisting}\\
      \hline
      \texttt{toUpper()}& Retourne le même caractère mais en majuscules. & toto \\
      \end{tabular}
      \end{center}
   \end{enumerate}
\end{document}

I can deduce that the problem is because the table is in a item of an enumeration.

Is there a way to solve this ?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Jérôme
  • 26,567
  • 29
  • 98
  • 120
  • Unfortunately, I am not able to reproduce your error. I've tested with the article-documentclass and the snippet you provided, but there wasn't any indent. Anyway, you might try to write `\noindent\begin{lstlisting}` which avoids paragraph indentation (must be used at the beginning). – tux21b Jun 30 '10 at 14:18
  • Same here, no indent. Can we see your preamble? – Joseph Wright Jun 30 '10 at 18:12
  • @tux21 and @Joseph Wright : I updated my question. My first code snippet didn't show my table was part of an item in an enumeration. This is the source of the problem, but I don't know how to solve it. – Jérôme Jul 01 '10 at 06:48
  • 1
    Image link is broken. Re-upload? – Nathaniel M. Beaver Jul 22 '19 at 19:11

3 Answers3

14

The problem is caused by the table environment. By default latex inserts a small space in front of a row. This can be avoided by using the code @{} before the first column specification.

\begin{tabular}{@{}lp{5cm}l}
...
\end{tabular}

For more information about @{} see this link.

Your full code example will then be

\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern,textcomp}
\usepackage[frenchb]{babel}
\usepackage{listings}

\begin{document}
   \begin{enumerate}
   \item Par exemple :
      \begin{center}
      \begin{tabular}{@{}lp{5cm}l}
      \hline
      Méthode & Description & Exemple d'utilisation\\
      \hline
      \texttt{isLetter()}& Indique si le caractère est une lettre de l'alphabet. &
      \begin{lstlisting}[numbersep=0pt]
QChar MyChar('x');
bool IsLetter = MyChar.isLetter();
// IsLetter vaut vrai
QChar MyChar2('&');
IsLetter = MyChar2.isLetter();
// IsLetter vaut faux
      \end{lstlisting}\\
      \hline
      \texttt{toUpper()}& Retourne le même caractère mais en majuscules. & toto \\
      \end{tabular}
      \end{center}
   \end{enumerate}
\end{document}
midtiby
  • 14,550
  • 6
  • 34
  • 43
  • @midtiby : many thanks for the tip, I didn't know about `@{}`. It doesn't solve my actual problem though (but tux21b does). Thanks ! – Jérôme Jul 01 '10 at 08:37
8

Yes, the margin is indeed coming from the enumeration. But luckily, the package documentation of the listing package notes:

resetmargins= true|false (default: false)

If true, indention from list environments like enumerate or itemize is reset, i.e. not used.

Therefore, the following should help:

\begin{lstlisting}[numbersep=0pt,resetmargins=true]

Regards,
Christoph

Community
  • 1
  • 1
tux21b
  • 90,183
  • 16
  • 117
  • 101
0

Another approach is to use a negative left margin.

\begin{enumerate}[
        xleftmargin=-40pt %the 40pt may not apply depending on your environment
    ]
    % ... your enumeration here ...
\end{enumerate}

(I was using this for code snippet listings)

Cadoiz
  • 1,446
  • 21
  • 31