2

I am using lstlisting as part of a large document, and I wish to present line numbers. I am using the following code:

\begin{lstlisting}[language=C++, numbers=left, frame=single, escapechar=@]
int a = 1; // first line @\label{demo:first_line}@
int b = 2; // second line @\label{demo:second_line}@
int c = 3; // third line @\label{demo:third_line}@
\end{lstlisting}
a appears in line \ref{demo:first_line}.
b appears in line \ref{demo:second_line}.
c appears in line \ref{demo:third_line}.

However, only a single number appears on the top left corner (changing the "numbers" flag to "right" will move it to the top right corner). Also, the frame appears to be broken and references to line numbers are all 0. See picture:

enter image description here

I copied the same code to a clean document, and everything worked fine:

enter image description here

EDIT: The following code reproduces the problem:

custom.cls:

\def\filename{custom}
\LoadClass{article}
\RequirePackage[hebrew,english]{babel}
\endinput

main.tex:

\documentclass{custom}
\usepackage{listings}
\begin{document}

\begin{lstlisting}[language=C++, numbers=left, frame=single, escapechar=@]
int a = 1; // first line @\label{demo:first_line}@
int b = 2; // second line @\label{demo:second_line}@
int c = 3; // third line @\label{demo:third_line}@
\end{lstlisting}
a appears in line \ref{demo:first_line}.
b appears in line \ref{demo:second_line}.
c appears in line \ref{demo:third_line}.
\end{document}
6
  • 3
    Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – Dai Bowen Nov 2 '17 at 12:27
  • Thanks for the useful link about (MWEs). While minimizing, I already found the problem. – user1656308 Nov 2 '17 at 14:18
  • 1
    Then, what was the problem? – daleif Nov 2 '17 at 14:21
  • I edited the original question to include the solution as well. A "\RequirePackage[hebrew,english]{babel}" command inside the cls was the source of the problem. I did not actually need that package in the document, so I simply removed it. – user1656308 Nov 2 '17 at 14:27
  • 2
    We'd like to keep answers separate from questions, so you should write a separate answer instead of editing your answer into the question. Self-answers are perfectly admissible, and a well-written answer may earn you additional reputation. – CarLaTeX Nov 2 '17 at 14:49
2

The "\RequirePackage[hebrew,english]{babel}" command inside the cls file is the source of the problem. Removing it resolves the problem.

More specifically, the problem is with "hebrew". Relacing the command with "\RequirePackage[english]{babel}" also resolved the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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