4

In this code, I would like automatic hide the first line (in my complete example, the code is inserted from an external file).

A solution is to use firstline=2, but then it will hide the first even if this line is not a coding line.

The solution is probably with the morecomment=[is]{}{} command, but I don't understand how to do that.

\documentclass[a4paper,11pt]{article}

%compilation XeLaTeX
%\usepackage[tuenc]{fontspec}
%
%compilation LaTeX
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage[french]{babel}
\usepackage{listings}

\lstset{language=Python,morecomment=[is]{\#  -*-}{-*-}}
\begin{document}

\begin{lstlisting}
# -*- coding: utf-8 -*-   <- this line should no appear
Python code
\end{lstlisting}

\end{document}
  • I don't understand. Would you like hide the first line if and only if it is comment line? – user91669 Dec 2 '17 at 23:12
  • Only if it is a line same as "# -- coding: ....... -- ", or if this is not possible, only if the line begins with # -- and end with -- – Oli Dec 3 '17 at 8:39
1

Here's a version similar to yours with delimiters # -*- and ^^M. The latter corresponds to the end of line character. Luckily, the end of line is eaten completely by the input processor, so that no spurious empty line remains in the output.

\documentclass[a4paper,11pt]{article}

\usepackage{listings}
\lstset{
    language=Python,
    frame=single,
    numbers=left,
    morecomment=[is]{\#\ -*-}{\^^M}
}
\begin{document}
\begin{lstlisting}
# -*- coding: utf-8 -*-   <- this line should no appear
Python code
\end{lstlisting}

\begin{lstlisting}
# coding: utf-8    <- this line *should* appear
Python code
\end{lstlisting}
\end{document}

enter image description here

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.