Am Mittwoch, den 08.08.2018, 08:53 +0100 schrieb José Abílio Matos:
> Consider the following tex file:
> 
> \documentclass{scrbook} 
> \usepackage{listings}
> \lstset{language=Octave}
> 
> \begin{document}
> \begin{lstlisting}
>   [x,y] = nice_fun(a,b);
> \end{lstlisting}      
> \end{document}
> 
> The problem is that when importing tex2lyx reads the [x,y] as an
> additional 
> arguments to lstlisting.

My testing revealed that

\begin{lstlistings}
[foo]

reads [foo] as content, whereas

\begin{lstlistings}%
[foo]

or

\begin{lstlistings}[foo,
bar]

reads [foo...] as argument.

Patch attached. Please test.

It's possible that minted needs the same fix. I have not tested (I
don't have minted installed).

Jürgen

> 
> Is this known? Should I add a new tracker issue?
> 
> Regards,
diff --git a/src/tex2lyx/text.cpp b/src/tex2lyx/text.cpp
index c334de5c5c..1b9cb89e39 100644
--- a/src/tex2lyx/text.cpp
+++ b/src/tex2lyx/text.cpp
@@ -729,7 +729,8 @@ InsetLayout const * findInsetLayout(TextClass const & textclass, string const &
 }
 
 
-void eat_whitespace(Parser &, ostream &, Context &, bool);
+void eat_whitespace(Parser &, ostream &, Context &, bool eatParagraph,
+		    bool eatNewline = true);
 
 
 /*!
@@ -1998,7 +1999,17 @@ void parse_environment(Parser & p, ostream & os, bool outer,
 
 	else if (name == "lstlisting" || name == "minted") {
 		bool use_minted = name == "minted";
-		eat_whitespace(p, os, parent_context, false);
+		// with listings, we do not eat newlines here since
+		// \begin{lstlistings}
+		// [foo]
+		// reads [foo] as content, whereas
+		// // \begin{lstlistings}%
+		// [foo]
+		// or
+		// \begin{lstlistings}[foo,
+		// bar]
+		// reads [foo...] as argument.
+		eat_whitespace(p, os, parent_context, false, use_minted);
 		if (use_minted && minted_float.empty()) {
 			// look ahead for a bottom caption
 			p.pushPosition();
@@ -2264,17 +2275,18 @@ void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
  * Reads spaces and comments until the first non-space, non-comment token.
  * New paragraphs (double newlines or \\par) are handled like simple spaces
  * if \p eatParagraph is true.
+ * If \p eatNewline is false, newlines won't be treated as whitespace.
  * Spaces are skipped, but comments are written to \p os.
  */
 void eat_whitespace(Parser & p, ostream & os, Context & context,
-		    bool eatParagraph)
+		    bool eatParagraph, bool eatNewline)
 {
 	while (p.good()) {
 		Token const & t = p.get_token();
 		if (t.cat() == catComment)
 			parse_comment(p, os, t, context);
-		else if ((! eatParagraph && p.isParagraph()) ||
-			 (t.cat() != catSpace && t.cat() != catNewline)) {
+		else if ((!eatParagraph && p.isParagraph()) ||
+			 (t.cat() != catSpace && (t.cat() != catNewline || !eatNewline))) {
 			p.putback();
 			return;
 		}

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to