BUG: Fix to_string output when using header keyword arg (#16718)
State: closed

Also affects to_latex midrule position

- [x] closes #16718
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry

Fixes the 4th example in the above issue.

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

Comments:
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/25602?src=pr&el=h1) Report
> Merging [#25602](https://codecov.io/gh/pandas-dev/pandas/pull/25602?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/5c159d2ea2450cc6501722c13b916581706d19fd?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/25602/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/25602?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master   #25602   +/-   ##
=======================================
  Coverage   91.26%   91.26%           
=======================================
  Files         173      173           
  Lines       52968    52968           
=======================================
  Hits        48339    48339           
  Misses       4629     4629
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `89.83% <100%> (ø)` | :arrow_up: |
| #single | `41.71% <33.33%> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/25602?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/io/formats/format.py](https://codecov.io/gh/pandas-dev/pandas/pull/25602/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvZm9ybWF0LnB5) | `97.99% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/25602?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/25602?src=pr&el=footer). Last update [5c159d2...bc37b34](https://codecov.io/gh/pandas-dev/pandas/pull/25602?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/25602?src=pr&el=h1) Report
> Merging [#25602](https://codecov.io/gh/pandas-dev/pandas/pull/25602?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/9185af206b1e9e07d3b6f5a556b32862624fb100?src=pr&el=desc) will **decrease** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/25602/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/25602?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #25602      +/-   ##
==========================================
- Coverage   91.29%   91.29%   -0.01%     
==========================================
  Files         173      173              
  Lines       52961    52961              
==========================================
- Hits        48350    48349       -1     
- Misses       4611     4612       +1
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `89.86% <100%> (ø)` | :arrow_up: |
| #single | `41.73% <33.33%> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/25602?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/io/formats/format.py](https://codecov.io/gh/pandas-dev/pandas/pull/25602/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvZm9ybWF0LnB5) | `97.99% <100%> (ø)` | :arrow_up: |
| [pandas/util/testing.py](https://codecov.io/gh/pandas-dev/pandas/pull/25602/diff?src=pr&el=tree#diff-cGFuZGFzL3V0aWwvdGVzdGluZy5weQ==) | `88.98% <0%> (-0.1%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/25602?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/25602?src=pr&el=footer). Last update [9185af2...58c9346](https://codecov.io/gh/pandas-dev/pandas/pull/25602?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

I've added a test for `to_latex` and a whatsnew entry. Let me know if you want me to change anything else.
Thanks @tomneep!