Fixed LatexFormatter with MultIndex index
State: closed

Used `MultiIndex.formatter()` to correctly sparsify the index for `to_latex()`.

I was having an issue with printing Latex tables. This can be seen in the comparison of `to_string()` and `to_latex()` for the following table. Notice that the first row of `Vol8` should not be missing any labels because it marks a new label in the outermost level. This is correct in `to_string` and incorrect in `to_latex`.

Output of `to_string()`:
```
Volunteer Visit Stim  Marker             
Vol6      2     Long  CD137+           96
                      IFNg+           192
                Short CD154+           96
          14    Long  IFNg+           288
                Short CD137+          192
Vol7      2     Long  CD69-            96
                      IFNg+            96
                Short CD137+           96
          4     Short CD137+          192
                      IFNg+            96
          14    Short CD137+           96
                      CD154+           96
                      IFNg+            96
Vol8      2     Short CD137+          288
          4     Long  IFNg+           192
```

Output of `to_latex()`:
```
\begin{tabular}{llllr}
\toprule
     &    &      &        &  Total cells \\
Volunteer & Visit & Stim & Marker &              \\
\midrule
Vol6 & 2  & Long & CD137+ &           96 \\
     &    &      & IFNg+ &          192 \\
     &    & Short & CD154+ &           96 \\
     & 14 & Long & IFNg+ &          288 \\
     &    & Short & CD137+ &          192 \\
Vol7 & 2  & Long & CD69- &           96 \\
     &    &      & IFNg+ &           96 \\
     &    & Short & CD137+ &           96 \\
     & 4  &      &        &          192 \\
     &    &      & IFNg+ &           96 \\
     & 14 &      & CD137+ &           96 \\
     &    &      & CD154+ &           96 \\
     &    &      & IFNg+ &           96 \\
Vol8 & 2  &      & CD137+ &          288 \\
     & 4  & Long & IFNg+ &          192 \\
\bottomrule
\end{tabular}
```

I've tested the relevant pieces of this code using this example, but I would be grateful for help with adding tests to the repository. I'm not set up to develop the `pandas` codebase currently and am not exactly sure how to add the right tests in the right places. Thanks!

 - [x] Addresses issue #15480 
 - [x] tests added / passed
 - [ ] passes ``git diff upstream/master | flake8 --diff``

Labels:
name: Output-Formatting
description: __repr__ of pandas objects, to_string
name: IO LaTeX
description: to_latex

Comments:
Tests will go in `pandas/tests/formats/test_formats.py`, you'll see some other latex tests there.

Could you also post screen shots of the output before and after your fix? A release note too in `doc/source/whatsnw/v0.20.0.txt`
this is going to need tests
can you rebase / update
can you add the test from the issue? and a whatsnew note (bug fixes is fine).

tests are now in ``pandas/tests/formats/test_to_latex.py``
I merged from `upstream/master` where upstream is `pandas-dev/pandas`. Is that what you meant? I apologize that I have done nothing with this recently. I'll try to get to it soon.
Yes, I should be able to generate the test pretty easily. I'll let you know if I need more help.
@agartland yep, that's what I meant.
can you rebase / update
@agartland we're releasing 0.20.2 in a few days, if you want to rebase and update.
OK I've added a test and a whatsnew note. I will wait to see if the new test passes the CI.
Let me know if I need to do anything else. Sorry for the delay.

## Before patch:
![image](https://cloud.githubusercontent.com/assets/3869734/26657307/e73f504c-4617-11e7-9c6a-bab9f9fc84b2.png)

## After patch:
![image](https://cloud.githubusercontent.com/assets/3869734/26657312/f0e6f5a0-4617-11e7-9202-e1727cdc0673.png)

Looks like m patch broke these two tests:
```TestToLatex.test_to_latex_multiindex
TestToLatex.test_to_latex_multicolumnrow
```
They both fail because they expect a black value to look like:
` 5 &  & 4`
but instead it is:
` 5 & {} & 4`

I'm not sure if there's a difference in Latex for these two. The `{}` comes from code that I didn't write, but now they are inserted in different places. Should I update the tests with `{}` so they pass?
@agartland the outputs do look the same to be. Could you do a bit of digging to see who's inserting the `{}` though? At a glance, it seems to be from LateFormatter.write_result. If that's right, then we should just update the tests.

I think https://travis-ci.org/pandas-dev/pandas/jobs/238131999#L1144 is a real failure though?  Here it is, using pandoc, assuming I copy-pasted correctly:

```
---
header-includes:
    - \usepackage{booktabs}
    - \usepackage{multirow}
---

# Old

\begin{tabular}{ll}
    \toprule
    {} &  x \\
    {} &  y \\
    \midrule
    0 &  a \\
    \bottomrule
\end{tabular}

# New

\begin{tabular}{lll}
    \toprule
      &   &  0 \\
    \midrule
    x & y &  a \\
    \bottomrule
\end{tabular}
```

![screen shot 2017-06-01 at 7 41 15 am](https://cloud.githubusercontent.com/assets/1312546/26680131/bbf99a60-469d-11e7-99c5-30e5929447e6.png)


closing as stale. if you want to update. pls comment.