Basically I would claim what you see is a) a bug in listings and be deficiency in "here" floats in LaTex2e and probably a bug in the implementation of equation
environment when it comes to the fleqn
option. The parskip
package is actually not doing much here and for testing I would just comment it out and use \setlength\parskip{33pt}
instead.
If you do this, then you see parskip being inserted
- around the equation environment (bug?)
- only after the lstlisting environment (definitely bug)
- not around the float (but the spacing there is uneven - deficiency)
If you additionally remove the fleqn
option the space around equation vanishes (and indeed there shouldn't be parskip around it). Not much we can do here on this one other than to suggest using amsmath
which has a proper implementation for fleqn
.
Concerning listings, I would claim this is a bug of the environment as it always adds some space above and below (controllable through settings) but in addition always adds a \parskip
after and does so regardless of whether this environment is surrounded by blank lines or not. A workaround would be to configure the "belowskip" to be whatever "aboveskip" is minus the parskip setting, e.g.
\setlength\parskip{33pt} % our strange value
\lstset{aboveskip=6pt plus 2pt, belowskip=-27pt plus 2pt}
Finally the float: here the is not much you can do: if the float is in vertical mode (which it isn't in your example) then you can apply my answer in intextsep-gives-doubled-space
otherwise the best I can suggest is to place a \strut
in front, e.g. something like
XXXXXXXXXXX XXXXXXXXXX XXXXXXXXXXX XXXXX XXXXX
\strut
\begin{figure}[h]
\rule{10cm}{2cm}
\end{figure}
CCCXXXXXXX XXXXXXXXXX XXXXXXXXXXX XXXXX XXXXX
If you use \showoutput
in the preamble you will see that LaTeX properly puts just \intexsep
above and below the float and nothing else. However, after the float there is additionally the calculated space added to reach the next baseline. And that amount depends on whether or not the previous line as descenders (if not there there is more space added). So the strut helps to even this out.
Technically there is no way for LaTeX (well, it is a Turing machine, but with realistic effort) to place the float in the middle of the calculated space as this information is not yet known when the float is added.
Update
One way to at least automate the addition of the \strut
in horizontal mode is to use the following patch:
\usepackage{etoolbox}
\makeatletter
\patchcmd{\end@float}%
{\vadjust}{\strut\vadjust}%
{\typeout{*** SUCCESS ***}}{\typeout{*** FAIL ***}}
\makeatother
Your table code is quite unusual. I would rather write your table on the following way:
\documentclass[]{article}
\usepackage{siunitx} % new, used is version v3.0.32, last revised 2021-09-29.
\usepackage{booktabs, tabularx} % new
\usepackage[referable]{threeparttablex} % new
\begin{document}
\begin{table}[ht]
\begin{threeparttable}
\caption{A nice table }
\label{tab:may beautiful table}
\sisetup{table-format = {(}-1.3, % <---
input-open-uncertainty = ,
input-close-uncertainty = ,
table-align-text-before = false, % <---
table-space-text-post =$^{***}$ % <---
}
\small
\setlength\tabcolsep{1pt}
\begin{tabularx}{\linewidth}{@{} X *{3}{S} @{\hspace{1em}} *{3}{S} @{}}
\toprule
& \multicolumn{6}{c}{\textit{Dependent variable:}} \\
\cmidrule(l){2-7}
& {(1)} & {(2)} & {(3)} & {(4)} & {(5)} & {(6)} \\
\midrule
Medicine A
& 0.999\tnote{**} & -0.888 & 0.777\tnote{**} % <---
& 0.666\tnote{***} & 0.555 & 0.444 \\
& (0.111) & (-0.222) & (0.333) % <---
& (0.444) & (0.555) & (0.666) \\
\addlinespace
Medicine B
& 1.555 & 1.666\tnote{***} & 1.777
& 1.888\tnote{***} & 1.999 & 2.000\tnote{*} \\
& (0.111) & (0.222) & (0.333)
& (0.444) & (0.555) & (0.666) \\
\addlinespace
Observations
& {200000} & {200000} & {200000}
& {200000} & {200000} & {200000} \\
Adjusted $\mathrm{R}^{2}$
& {0.50} & {0.50} & {0.50}
& {0.50} & {0.50} & {0.50} \\
\bottomrule
\end{tabularx}\footnotesize
\begin{tablenotes}[para]
\note: \item[*:] $p<0.1$; \item[**:] $p<0.05$; \item[***:] $p<0.05$
\end{tablenotes}
\end{threeparttable}
\end{table}
\end{document}
Edit:
Added example of use sign -
in table.
Best Answer
Note that the main
tabular
has the first column alignment specified aswhich removes the column spacing to the left of the
l
eft-aligned column (due to@{}
).\multicolumn{.}{l}
re-formats the column specification tol
without the removal of the column spacing on the left. So you should either useor