http://bugzilla.lyx.org/show_bug.cgi?id=1290

This patch is on bugzilla for a while, but since there's no discussion I 
thought I'd bring it up here.

As you know, LyX does not center (in general: align) float contents (figures, 
tabulars) right, because \begin{center}...\end{center} is implemented as a 
trivlist and adds some unwanted space.

LaTeXers usually recommend to use \centering inside floats to avoid this 
problem:
http://www.tex.ac.uk/cgi-bin/texfaq2html?label=vertspacefloat

However, \begin{centering}...\end{centering} is basically the same, but is 
better markup.

The patch checks if we are inside a float/wrap and uses 
\begin{centering}...\end{centering} etc. if true and 
\begin{center}...\end{center} etc. if false.

There are still problems with aligning contents in tabular cells (especially 
multiple pars in fixed width cells), but as discussed recently on this list, 
this needs a different fix.

I think this issue must be fixed. What is your opinion on the patch?
Juergen.
Index: src/paragraph.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/paragraph.C,v
retrieving revision 1.318
diff -u -r1.318 paragraph.C
--- src/paragraph.C	9 Sep 2003 22:13:28 -0000	1.318
+++ src/paragraph.C	12 Sep 2003 06:06:07 -0000
@@ -687,6 +687,11 @@


 // This could go to ParagraphParameters if we want to
+// \begin{center} and \begin{centering} are different
+// since the former is a trivlist and adds some vertical space
+// which is unwanted inside floats and tabular cells
+// therefor we have to use both. Same counts for
+// \begin{flushright} and \begin{raggedleft} etc.
 int Paragraph::startTeXParParams(BufferParams const & bparams,
 				 ostream & os, bool moving_arg) const
 {
@@ -721,24 +726,39 @@
 		break;
 	case LYX_ALIGN_LEFT:
 		if (getParLanguage(bparams)->babel() != "hebrew") {
-			os << "\\begin{flushleft}";
+			if (noTrivlistCentering())
+				os << "\\begin{raggedright}";
+			else
+				os << "\\begin{flushleft}";
 			column += 17;
 		} else {
-			os << "\\begin{flushright}";
+			if (noTrivlistCentering())
+				os << "\\begin{raggedleft}";
+			else
+				os << "\\begin{flushright}";
 			column += 18;
 		}
 		break;
 	case LYX_ALIGN_RIGHT:
 		if (getParLanguage(bparams)->babel() != "hebrew") {
-			os << "\\begin{flushright}";
+			if (noTrivlistCentering())
+				os << "\\begin{raggedleft}";
+			else
+				os << "\\begin{flushright}";
 			column += 18;
 		} else {
-			os << "\\begin{flushleft}";
+			if (noTrivlistCentering())
+				os << "\\begin{raggedright}";
+			else
+				os << "\\begin{flushleft}";
 			column += 17;
 		}
 		break;
 	case LYX_ALIGN_CENTER:
-		os << "\\begin{center}";
+		if (noTrivlistCentering())
+			os << "\\begin{centering}";
+		else
+			os << "\\begin{center}";
 		column += 14;
 		break;
 	}
@@ -777,24 +797,39 @@
 		break;
 	case LYX_ALIGN_LEFT:
 		if (getParLanguage(bparams)->babel() != "hebrew") {
-			os << "\\end{flushleft}";
+			if (noTrivlistCentering())
+				os << "\\par\\end{raggedright}";
+			else
+				os << "\\par\\end{flushleft}";
 			column = 15;
 		} else {
-			os << "\\end{flushright}";
+			if (noTrivlistCentering())
+				os << "\\par\\end{raggedright}";
+			else
+				os << "\\par\\end{flushright}";
 			column = 16;
 		}
 		break;
 	case LYX_ALIGN_RIGHT:
 		if (getParLanguage(bparams)->babel() != "hebrew") {
-			os << "\\end{flushright}";
+			if (noTrivlistCentering())
+				os << "\\par\\end{raggedleft}";
+			else
+				os << "\\par\\end{flushright}";
 			column+= 16;
 		} else {
-			os << "\\end{flushleft}";
+			if (noTrivlistCentering())
+				os << "\\par\\end{raggedleft}";
+			else
+				os << "\\par\\end{flushleft}";
 			column = 15;
 		}
 		break;
 	case LYX_ALIGN_CENTER:
-		os << "\\end{center}";
+		if (noTrivlistCentering())
+			os << "\\par\\end{centering}";
+		else
+			os << "\\par\\end{center}";
 		column = 12;
 		break;
 	}
@@ -1329,6 +1364,15 @@
 ParagraphParameters const & Paragraph::params() const
 {
 	return pimpl_->params;
+}
+
+
+bool Paragraph::noTrivlistCentering() const
+{
+	if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
+		return (pimpl_->inset_owner->owner()->lyxCode() == InsetOld::FLOAT_CODE
+			|| pimpl_->inset_owner->owner()->lyxCode() == InsetOld::WRAP_CODE);
+	return false;
 }
 
 
Index: src/paragraph.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/paragraph.h,v
retrieving revision 1.103
diff -u -r1.103 paragraph.h
--- src/paragraph.h	6 Sep 2003 18:38:01 -0000	1.103
+++ src/paragraph.h	12 Sep 2003 06:06:08 -0000
@@ -287,6 +287,14 @@
 	///
 	int stripLeadingSpaces();
 
+	/**
+	 * In some environments, \centering or \begin{centering}...
+	 * \end{centering} (which is the same) should be used rather
+	 * than \begin{center}...\end{center} (which inserts some extra space)
+	 * Return true if we are inside one of those
+	 */
+	bool noTrivlistCentering() const;
+
 	/// return true if we allow multiple spaces
 	bool isFreeSpacing() const;
 

Reply via email to