João Luis Meloni Assirati wrote:

> I still think that this is impossible since unless the lyx format had a
> special syntax for unknown environments.

I still don't think that it is impossible. Proof attached ;-)

> Think of this possibility: 
> 
> \large some text
> \begin{unknown}
> \huge some other text
> \end{unknown}
> yet another text
> 
> "yet another text" must be large, not huge and not normal. Tex2lyx should
> then translate it to
> 
> \size large
> some text
> ERT(\begin{unknown})
> \size huge
> some other text
> \size normal
> ERT(\end{unknown})
> \size large
> yet another text
> 
> which is quite different in spirit to the original latex.

Indeed. The attached patch implements that. I could not break it. Please
test whether it works for you. Because it was now easy to implement I also 
removed some redundant font attribute changes.

>> We don't want that, because it tends to put whole files into one ERT
>> block.
> 
> I know, but this possibility is still better than failing. People would
> rather like to have parts of their hand made latex in ERT (since they
> already know latex) than having a lyx file that does not export to a valid
> latex.

Agreed.


Georg
Index: src/tex2lyx/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/tex2lyx/ChangeLog,v
retrieving revision 1.96
diff -u -p -r1.96 ChangeLog
--- src/tex2lyx/ChangeLog	9 Jun 2005 09:58:08 -0000	1.96
+++ src/tex2lyx/ChangeLog	11 Jul 2005 15:45:34 -0000
@@ -1,3 +1,11 @@
+2005-07-11  Georg Baum  <[EMAIL PROTECTED]>
+
+	* text.C (parse_text_snippet): reset font if needed
+	* text.C (parse_text): output font changes only if needed
+	* text.C (output_font_change): new, needed for the above
+	* text.C (parse_environment): reset font if needed
+	* text.C (environment_ert): new, needed for the above
+
 2005-06-01  Georg Baum  <[EMAIL PROTECTED]>
 
 	* text.C (parse_text): eat {} after \ss, \i and \j
Index: src/tex2lyx/text.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/tex2lyx/text.C,v
retrieving revision 1.59
diff -u -p -r1.59 text.C
--- src/tex2lyx/text.C	1 Jun 2005 14:01:18 -0000	1.59
+++ src/tex2lyx/text.C	11 Jul 2005 15:45:35 -0000
@@ -50,6 +51,24 @@ using std::vector;
 namespace fs = boost::filesystem;
 
 
+namespace {
+
+/// Output changed font parameters if \p oldfont and \p newfont differ
+void output_font_change(ostream & os, Font const & oldfont, Font const & newfont)
+{
+	if (oldfont.family != newfont.family)
+		os << "\n\\family " << newfont.family << '\n';
+	if (oldfont.series != newfont.series)
+		os << "\n\\series " << newfont.series << '\n';
+	if (oldfont.shape != newfont.shape)
+		os << "\n\\shape " << newfont.shape << '\n';
+	if (oldfont.size != newfont.size)
+		os << "\n\\size " << newfont.size << '\n';
+}
+
+}
+
+
 /// thin wrapper around parse_text using a string
 string parse_text(Parser & p, unsigned flags, const bool outer,
 		  Context & context)
@@ -70,7 +89,7 @@ void parse_text_in_inset(Parser & p, ost
 }
 
 
-/// parses a paragraph snippet, useful for example for \emph{...}
+/// parses a paragraph snippet, useful for example for \\emph{...}
 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
 		Context & context)
 {
@@ -79,6 +98,9 @@ void parse_text_snippet(Parser & p, ostr
 	parse_text(p, os, flags, outer, newcontext);
 	// should not be needed
 	newcontext.check_end_layout(os);
+	// Reset font if it was changed.
+	// This is needed e.g. for unknown environments.
+	output_font_change(os, newcontext.font, context.font);
 }
 
 
@@ -639,6 +639,28 @@ void parse_box(Parser & p, ostream & os,
 		}
 #endif
 	}
+
+
+/// begin or end an environment with ert and font reset
+void environment_ert(string const & name, ostream & os, Context & context,
+                     bool begin)
+{
+	static Font normalfont;
+	bool initialized = false;
+	if (!initialized) {
+		normalfont.init();
+		initialized = true;
+	}
+
+	// We need to reset the font for the ERT. Otherwise things like
+	// \large\begin{foo}\huge bar\end{foo}
+	// will not work.
+	output_font_change(os, context.font, normalfont);
+	if (begin)
+		handle_ert(os, "\\begin{" + name + "}", context);
+	else
+		handle_ert(os, "\\end{" + name + "}", context);
+	output_font_change(os, normalfont, context.font);
 }
 
 
@@ -774,15 +1124,15 @@ void parse_environment(Parser & p, ostre
 
 	else if (name == "tabbing") {
 		// We need to remember that we have to handle '\=' specially
-		handle_ert(os, "\\begin{" + name + "}", parent_context);
+		environment_ert(name, os, parent_context, true);
 		parse_text_snippet(p, os, FLAG_END | FLAG_TABBING, outer, parent_context);
-		handle_ert(os, "\\end{" + name + "}", parent_context);
+		environment_ert(name, os, parent_context, false);
 	}
 
 	else {
-		handle_ert(os, "\\begin{" + name + "}", parent_context);
+		environment_ert(name, os, parent_context, true);
 		parse_text_snippet(p, os, FLAG_END, outer, parent_context);
-		handle_ert(os, "\\end{" + name + "}", parent_context);
+		environment_ert(name, os, parent_context, false);
 	}
 
 	active_environments.pop_back();
@@ -1512,10 +1992,8 @@ void parse_text(Parser & p, ostream & os
 			os << "\n\\shape " << context.font.shape << "\n";
 			if (t.cs() == "textnormal") {
 				parse_text_snippet(p, os, FLAG_ITEM, outer, context);
+				output_font_change(os, context.font, oldFont);
 				context.font = oldFont;
-				os << "\n\\shape " << oldFont.shape << "\n";
-				os << "\n\\series " << oldFont.series << "\n";
-				os << "\n\\family " << oldFont.family << "\n";
 			} else
 				eat_whitespace(p, os, context, false);
 		}
@@ -1695,15 +2211,12 @@ void parse_text(Parser & p, ostream & os
 			char const * const * where =
 				is_known(t.cs(), known_old_font_families);
 			context.check_layout(os);
-			string oldsize = context.font.size;
+			Font const oldFont = context.font;
 			context.font.init();
-			context.font.size = oldsize;
+			context.font.size = oldFont.size;
 			context.font.family =
 				known_coded_font_families[where - known_old_font_families];
-			// FIXME: Only do this if it is necessary
-			os << "\n\\family " << context.font.family << "\n"
-			   <<   "\\series " << context.font.series << "\n"
-			   <<   "\\shape "  << context.font.shape  << "\n";
+			output_font_change(os, oldFont, context.font);
 			eat_whitespace(p, os, context, false);
 		}
 
@@ -1711,15 +2224,12 @@ void parse_text(Parser & p, ostream & os
 			char const * const * where =
 				is_known(t.cs(), known_old_font_series);
 			context.check_layout(os);
-			string oldsize = context.font.size;
+			Font const oldFont = context.font;
 			context.font.init();
-			context.font.size = oldsize;
+			context.font.size = oldFont.size;
 			context.font.series =
 				known_coded_font_series[where - known_old_font_series];
-			// FIXME: Only do this if it is necessary
-			os << "\n\\family " << context.font.family << "\n"
-			   <<   "\\series " << context.font.series << "\n"
-			   <<   "\\shape "  << context.font.shape  << "\n";
+			output_font_change(os, oldFont, context.font);
 			eat_whitespace(p, os, context, false);
 		}
 
@@ -1727,15 +2237,12 @@ void parse_text(Parser & p, ostream & os
 			char const * const * where =
 				is_known(t.cs(), known_old_font_shapes);
 			context.check_layout(os);
-			string oldsize = context.font.size;
+			Font const oldFont = context.font;
 			context.font.init();
-			context.font.size = oldsize;
+			context.font.size = oldFont.size;
 			context.font.shape =
 				known_coded_font_shapes[where - known_old_font_shapes];
-			// FIXME: Only do this if it is necessary
-			os << "\n\\family " << context.font.family << "\n"
-			   <<   "\\series " << context.font.series << "\n"
-			   <<   "\\shape "  << context.font.shape  << "\n";
+			output_font_change(os, oldFont, context.font);
 			eat_whitespace(p, os, context, false);
 		}
 

Reply via email to