4

I have some troubles with spacing when using minipages to keep some content together.

\documentclass[a4paper,10pt,notitlepage]{article}
\usepackage{geometry} 
\geometry{
    a4paper,
    head=20mm,
    left=20mm,
    right=10mm,
    top=25mm,
    bottom=25mm,
    foot=8mm
}
\usepackage[default,scale=0.95]{opensans} %Schriftart FEHLER
\usepackage{tabularx}
\usepackage{booktabs}                       % use package booktabs for easy creating tables
\usepackage{caption}
\usepackage{subcaption} 
\usepackage[table]{xcolor}
\usepackage{setspace}   
\usepackage{floatflt,epsfig} 
\usepackage{blindtext}
\usepackage[flushleft]{threeparttable}      % to have a dedicated notes section after tables
\usepackage{titlesec}

\setlength{\fboxsep}{0pt}%
\setlength{\fboxrule}{1pt}%
\setlength{\abovecaptionskip}{7pt}
\setlength{\belowcaptionskip}{7pt}
\setlength{\textfloatsep}{10pt}

\linespread{1.0}   
\setlength{\parindent}{0pt}                 % verhindert Absatzeinrueckung nach Bildern

\usepackage[framemethod=tikz]{mdframed}

% CAPTION STYLE
\DeclareCaptionFormat{labelstyle}{% <- needed
\setlength\fboxsep{3pt}% <- needed
\colorbox{blue}{{#1}}#2#3%
} 
\definecolor{blue}{HTML}{336699}
\captionsetup[table]{singlelinecheck=false, labelfont={color=white}}    % Rechtsbündige Tabellen und Bildbeschreibung
\captionsetup[figure]{format=labelstyle, labelfont={color=white}, 
justification=raggedright,% <- only needed if longer captions should not be justified
singlelinecheck=false% <- added
}

% SECTION STYLE
% -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
\titleformat{\section}
{\Large\bfseries\color{blue}}
{\thesection}{1em}{} %\titleformat{\section}{\Large\bfseries\color{blue}}{\thesection}{1em-Abstand}
\titleformat{\subsection}
{\large\bfseries\color{blue}}
{\thesubsection}{1em}{} %\titleformat{\section}{\Large\bfseries\color{blue}}{\thesection}{1em-Abstand}
\titleformat{\subsubsection}
{\bfseries\color{blue}}
{\thesubsubsection}{1em}{} %\titleformat{\section}{\Large\bfseries\color{blue}}{\thesection}{1em-Abstand}
% -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


\newcommand{\astroltable} { 
    \captionsetup{type=table, format=labelstyle}
    \arrayrulecolor{blue} 
    % \aboverulesep = 0mm 
    % \belowrulesep = 0mm 
    }

    \newenvironment{tableenv}[2]{
        \everypar{}
        \def\tablecaption{#1}
        \def\tablelabel{tab:#2}
        \begin{center}
            \begin{footnotesize} 
                \astroltable
                \begin{threeparttable}
    }{
            \end{threeparttable}
            \caption{\tablecaption}
            \label{\tablelabel}
        \end{footnotesize}
    \end{center}}

\begin{document}

\section{A section}
\begin{minipage}{\textwidth}
    \subsection{A subsection}
    \blindtext
    \begin{tableenv}{blubb}{blubb}
        \begin{tabularx}{\textwidth}{p{0.2\textwidth}p{0.25\textwidth}Xp{0.075\textwidth}p{0.075\textwidth}p{0.075\textwidth}p{0.075\textwidth}}
            \toprule
            \textbf{City}  & \textbf{Country} & \textbf{min.} & \textbf{typ.} & \textbf{max.} & \textbf{Rate} \\ \midrule
            New York             & US and A               & -             & 1.0             & -            & n/a            \\ \bottomrule
        \end{tabularx}
    \end{tableenv}
\end{minipage}

\strut
\blindtext
\begin{tableenv}{blubb2}{blubb2}
    \begin{tabularx}{\textwidth}{p{0.2\textwidth}p{0.25\textwidth}Xp{0.075\textwidth}p{0.075\textwidth}p{0.075\textwidth}p{0.075\textwidth}}
        \toprule
        \textbf{City}  & \textbf{Country} & \textbf{min.} & \textbf{typ.} & \textbf{max.} & \textbf{Rate} \\ \midrule
        New York             & US and A               & -             & 1.0             & -            & n/a            \\ \bottomrule
    \end{tabularx}
\end{tableenv}
\blindtext

\end{document}

enter image description here

One problem is the missing space above Table 1, this is introduced with the use of the titlesec package and seems to be caused if the minipage starts with a sectiontitle. Is there a way to correct this?
The other problem is the space between the caption of Table 1 and the following text which is different from Table 2.

po.pe
  • 329
  • I suspect your issue has something to do with this: Why is \parskip zero inside a minipage?. – Peter Grill Mar 04 '20 at 08:47
  • With the restoring of the \parskip I can address the 2nd issue, but it causes the skip before Table 2 behave the same way as Table 1 then no matter if I use the titlesec package or not. – po.pe Mar 04 '20 at 08:56
  • The minipage there is vertically centred so the section just sees a following line with very great height so has no chance to adjust the spacing for teh following subsection, and the subsection being in a box has no chance to detect the space coming from the section heading above. (the \strut at the start of the paragraph won't affect the spacing there as the usual baselineskip is in effect). The page won't break after a section heading or within the table, s do you need the minipage at all? – David Carlisle Mar 04 '20 at 09:23
  • I need the minipage to keep the text and the table together. What would be a proper way of grouping Latex elements (paragraphs, tables, figures etc.)? Manually adding \pagebreaks is not what I want to end up with in a 200 pages document. – po.pe Mar 04 '20 at 09:55
  • You might also consider using \vbox{...}. – John Kormylo Mar 04 '20 at 15:09

1 Answers1

2

I don't think that the issues you've encountered are all that closely related to the use of a minipage environment. Instead, I would argue, the issues appear to be related to shortcomings in the definitions of the \astroltable macro and the tableenv environment. The fact that you define the tabularx environments to contain 7 columns, but then only use 6 columns, doesn't exactly help either.

Does the layout shown in the following screenshot come closer to meeting your objectives?

enter image description here

\documentclass[a4paper,10pt,notitlepage]{article}

\usepackage{geometry}
\geometry{a4paper,
    head=20mm,foot=8mm,
    left=20mm,right=10mm,
    top=25mm,bottom=25mm,
}

\usepackage[default,scale=0.95]{opensans}

\usepackage{tabularx,booktabs} 
\usepackage{caption,subcaption}
% CAPTION STYLE
\DeclareCaptionFormat{labelstyle}{% 
    \setlength\fboxsep{3pt}%
    \colorbox{myblue}{{#1}}#2#3}
\captionsetup[table]{singlelinecheck=false,
    labelfont={color=white}}
\captionsetup[figure]{format=labelstyle,
    labelfont={color=white},
    justification=raggedright,
    singlelinecheck=false}


\usepackage{titlesec}
% SECTION STYLE
\titleformat{\section}{%
    \Large\bfseries\color{myblue}}{\thesection}{1em}{}
\titleformat{\subsection}{%
    \large\bfseries\color{myblue}}{\thesubsection}{1em}{}
\titleformat{\subsubsection}{%
    \bfseries\color{myblue}}{\thesubsubsection}{1em}{}


\usepackage[table]{xcolor}
\definecolor{myblue}{HTML}{336699}

\usepackage{setspace}
%%%%\linespread{1.0} % better: "\singlespacing"

%%%%\usepackage{floatflt,epsfig}  % shouldn't be needed

\usepackage{blindtext}
\usepackage[flushleft]{threeparttable}   
\usepackage[framemethod=tikz]{mdframed}

\setlength{\fboxsep}{0pt}%
\setlength{\fboxrule}{1pt}%

\setlength{\abovecaptionskip}{7pt}
\setlength{\belowcaptionskip}{10pt}
\setlength{\textfloatsep}{10pt}

\setlength{\parindent}{0pt} 

\newcommand{\astroltable}{%
    \captionsetup{type=table, format=labelstyle,
        skip=0.75\baselineskip} % <-- new (default is 1\baselineskip)
    \arrayrulecolor{myblue}}

\newenvironment{tableenv}[2]{% quite a few changes here
    \par\medskip
    \def\tablecaption{#1}
    \def\tablelabel{tab:#2}
    \footnotesize % \footnotesize is a macro, not an environment
    \astroltable
    \begin{threeparttable}
    }{%
    \caption{\tablecaption}
    \label{\tablelabel}
    \end{threeparttable} % place this *after* \caption and \label
    }

\begin{document}

\section{A section}
\subsection{A subsection}
\begin{minipage}{\textwidth}
    \blindtext
    \begin{tableenv}{blubb}{blubb}
    \begin{tabularx}{\textwidth}{%
         p{0.2\textwidth} p{0.3\textwidth} *{4}{X}}
    \toprule
    \textbf{City} & \textbf{Country} & 
    \textbf{min.} & \textbf{typ.} & 
    \textbf{max.} & \textbf{Rate} \\
    \midrule
    New York & US and A & -- & 1.0 & -- & n/a \\ 
    \bottomrule
    \end{tabularx}
    \end{tableenv}
\end{minipage}

    \blindtext
    \begin{tableenv}{blubb2}{blubb2}
    \begin{tabularx}{\textwidth}{%
         p{0.2\textwidth} p{0.3\textwidth} *{4}{X}}
    \toprule
    \textbf{City} & \textbf{Country} & 
    \textbf{min.} & \textbf{typ.} & 
    \textbf{max.} & \textbf{Rate} \\
    \midrule
    New York & US and A & -- & 1.0 & -- & n/a \\ 
    \bottomrule
    \end{tabularx}
    \end{tableenv}

    \blindtext
\end{document} 
Mico
  • 506,678
  • That's great! Can you quickly explain why the \par is needed inside the tableenv? – po.pe Mar 04 '20 at 12:39
  • @po.pe - I inserted \par mainly as a replacement for \everypar{}, whose purpose I'm afraid I don't understand. The \par ("paragraph break") directive is thrown in as a precaution, in case that there's no paragraph break immediately before start of the tableenv environment. (Aside: the \blindtext directive does insert a paragraph break after it ends.) – Mico Mar 04 '20 at 13:05
  • @Mico - \everypar{code} executes the code at the start of every paragraph. @afterheading uses it to not indent the next paragraph (then shut itself off). wrapfig uses it to add \parshape to each paragraph. – John Kormylo Mar 04 '20 at 15:15
  • @JohnKormylo - Thanks for this explanation. So, what does \everypar{} do? Does it force a paragraph break? Might it have advantages over just running \par (as I propose doing in my answer)? – Mico Mar 04 '20 at 16:43
  • @JohnKormylo - Thanks. My question was much more basic (simple-minded?): What does \everypar{} do? – Mico Mar 05 '20 at 04:38
  • 1
    \everypar{} (no code) erases the previous \everypar definition. – John Kormylo Mar 05 '20 at 04:39