Index: plugins/latex/xp/ie_exp_LaTeX.cpp
===================================================================
--- plugins/latex/xp/ie_exp_LaTeX.cpp	(revision 23299)
+++ plugins/latex/xp/ie_exp_LaTeX.cpp	(working copy)
@@ -238,6 +238,8 @@
 	bool           m_bInSymbol;
  	bool	m_bInCourier;
  	bool	m_bInSansSerif;
+	bool				m_bInEndnote;
+	bool				m_bHaveEndnote;
   
 	JustificationTypes  m_eJustification;
 	bool				m_bLineHeight;
@@ -257,7 +259,7 @@
 
 void s_LaTeX_Listener::_closeParagraph(void)
 {
-	if ((!m_bInCell) && (!m_bInFootnote)) m_pie->write("\n");
+	if ((!m_bInCell) && (!m_bInFootnote) && (!m_bInEndnote)) m_pie->write("\n");
 	m_bInHeading = false;
 	return;
 }
@@ -291,7 +293,7 @@
 
 void s_LaTeX_Listener::_closeBlock(void)
 { 
-	if(m_bInFootnote)
+	if(m_bInFootnote || m_bInEndnote)
 		return;
 	if (!m_bInBlock)
 		return;
@@ -1205,7 +1207,9 @@
 	m_bInSymbol(0),
 	m_bInCourier(0),
 	m_bInSansSerif(0),
-	m_bFirstSection(true)
+	m_bFirstSection(true),
+	m_bInEndnote(false),
+	m_bHaveEndnote(false)
 {
 	m_pie->write("%% ================================================================================\n");
 	m_pie->write("%% This LaTeX file was created by AbiWord.                                         \n");
@@ -1234,6 +1238,12 @@
 	m_pie->write("%% Please set your language here\n");
 	m_pie->write("\\usepackage[english]{babel}\n");
 	m_pie->write("\\usepackage{color}\n");
+	/* TODO: Decice whether it is necessary to include the endnotes package
+	 * after the whole document is parsed. This is doable if we can extend
+	 * the interface of IE_Exp_LaTeX by a method that can insert text at the
+	 * beginning of the stream.
+	 */
+	m_pie->write("\\usepackage{endnotes}\n");
 	// Must be as late as possible.
 	m_pie->write("\\usepackage{hyperref}\n");
 
@@ -1259,7 +1269,8 @@
 	_closeSection();
 	_handleDataItems();
 	DELETEP(m_pTableHelper);
-
+	if (m_bHaveEndnote)
+		m_pie->write("\n\\theendnotes");
 	m_pie->write("\n\\end{document}\n");
 }
 
@@ -1389,7 +1400,6 @@
 
 	switch (pcrx->getStruxType())
 	{
-	case PTX_SectionEndnote:
 	case PTX_Section:
 	{
 		_closeSpan();
@@ -1508,8 +1518,32 @@
 		m_pie->write("\\footnote{");
 		return true;
 	}
-
+	
+	case PTX_SectionTOC:
+	{
+		_closeSpan();	
+		_closeBlock();
+		/*
+		_closeSection();
+		 */
+		m_pie->write("\\tableofcontents \n");
+	}
+	case PTX_EndTOC:
+		return true;
+	
+	case PTX_SectionEndnote:
+	{
+		m_bInEndnote = true;
+		m_pie->write("\\endnote{");
+		m_bHaveEndnote = true;
+		return true;
+	}
 	case PTX_EndEndnote:
+	{
+		m_bInEndnote = false;
+		m_pie->write("} ");
+		return true;
+	}
 	default:
 		UT_ASSERT(UT_TODO);
 		return true;