Repository: statsmodels/statsmodels

Issue:
remove brackets from caption in latex output
These brackets in the output of `.as_latex()` were causing the latex output to not compile if you added a caption using `table.add_title()`


Discussion:

[![Coverage Status](https://coveralls.io/builds/11683911/badge)](https://coveralls.io/builds/11683911)

Coverage remained the same at 90.426% when pulling **a578c28b81fc2490eb0a0d7d16e326cc71d28c49 on natashawatkins:fix-latex-summary2-caption** into **aed3187b22ff176e0ced89686e7305ab2bc5b63a on statsmodels:master**.


Resolving patch:
diff --git a/statsmodels/iolib/summary2.py b/statsmodels/iolib/summary2.py
--- a/statsmodels/iolib/summary2.py
+++ b/statsmodels/iolib/summary2.py
@@ -8,6 +8,7 @@
 import textwrap
 from .table import SimpleTable
 from .tableformatting import fmt_latex, fmt_txt
+import re
 
 
 class Summary(object):
@@ -16,6 +17,7 @@ def __init__(self):
         self.settings = []
         self.extra_txt = []
         self.title = None
+        self._merge_latex = False
 
     def __str__(self):
         return self.as_text()
@@ -203,8 +205,9 @@ def as_latex(self):
         tables = self.tables
         settings = self.settings
         title = self.title
+
         if title is not None:
-            title = '\\caption{' + title + '} \\\\'
+            title = '\\caption{' + title + '}'
         else:
             title = '\\caption{}'
 
@@ -212,6 +215,13 @@ def as_latex(self):
         tab = [x.as_latex_tabular() for x in simple_tables]
         tab = '\n\\hline\n'.join(tab)
 
+        to_replace = ('\\\\hline\\n\\\\hline\\n\\\\'
+        'end{tabular}\\n\\\\begin{tabular}{.*}\\n')
+
+        if self._merge_latex:
+             # create single tabular object for summary_col
+            tab = re.sub(to_replace,'\midrule\n\midrule\n', tab)
+
         out = '\\begin{table}', title, tab, '\\end{table}'
         out = '\n'.join(out)
         return out
@@ -498,6 +508,7 @@ def summary_col(results, float_format='%.4f', model_names=[], stars=False,
     summ = summ.fillna('')
 
     smry = Summary()
+    smry._merge_latex = True
     smry.add_df(summ, header=True, align='l')
     smry.add_text('Standard errors in parentheses.')
     if stars:


Test patch:
diff --git a/statsmodels/iolib/tests/test_summary2.py b/statsmodels/iolib/tests/test_summary2.py
new file mode 100644
--- /dev/null
+++ b/statsmodels/iolib/tests/test_summary2.py
@@ -0,0 +1,53 @@
+import warnings
+from numpy.testing import assert_equal, assert_
+from statsmodels.regression.linear_model import OLS, add_constant
+from statsmodels.iolib.summary2 import summary_col
+
+
+class TestSummaryLatex(object):
+
+    def test_summarycol(self):
+        # Test for latex output of summary_col object
+        desired = r'''
+\begin{table}
+\caption{}
+\begin{center}
+\begin{tabular}{lcc}
+\hline
+      &   y I    &   y II    \\
+\midrule
+\midrule
+const & 7.7500   & 12.4231   \\
+      & (1.1058) & (3.1872)  \\
+x1    & -0.7500  & -1.5769   \\
+      & (0.2368) & (0.6826)  \\
+\hline
+\end{tabular}
+\end{center}
+\end{table}
+'''
+        x = [1,5,7,3,5]
+        x = add_constant(x)
+        y1 = [6,4,2,7,4]
+        y2 = [8,5,0,12,4]
+        reg1 = OLS(y1,x).fit()
+        reg2 = OLS(y2,x).fit()
+        actual = summary_col([reg1,reg2]).as_latex()
+        actual = '\n%s\n' % actual
+        assert_equal(desired, actual)
+
+    def test_OLSsummary(self):
+        # Test that latex output of regular OLS output still contains
+        # multiple tables
+
+        x = [1,5,7,3,5]
+        x = add_constant(x)
+        y1 = [6,4,2,7,4]
+        reg1 = OLS(y1,x).fit()
+        with warnings.catch_warnings():
+            warnings.simplefilter("ignore")
+            actual = reg1.summary().as_latex()
+        string_to_find = r'''\end{tabular}
+\begin{tabular}'''
+        result = string_to_find in actual
+        assert(result is True)