0

I am generating tex documents automatically (HTML -> pandoc(tex) -> postprocess -> XELATEX), and my general table environment is tabularx (with usage of package ltablex, so these are longtables effectively).

One reason for this choice is the possibility to assign captions consistently (using package caption).

Sometimes need outer tabular's that should display the inner nested tabularx's with caption side-by-side.

My current result is: enter image description here which is created with this file

\documentclass[a4paper,dvipsnames]{article}
\usepackage[ngerman]{babel}
\usepackage[paper=portrait,pagesize]{typearea}
\usepackage{color}
\usepackage[table]{xcolor}
\definecolor{tableHighlightRowBackground}{gray}{0.9375}
\definecolor{tablecaptionfontcolor}{RGB}{108, 117, 125}
\definecolor{arrayrulecolor}{RGB}{222,226,230}

\usepackage[most]{tcolorbox}

\usepackage{relsize}

\usepackage{ltablex} \keepXColumns \usepackage{multirow} \usepackage{caption} \DeclareCaptionFont{muted}{\color{tablecaptionfontcolor}} \captionsetup[table]{singlelinecheck=false, justification=raggedleft, font={muted,small}}

\setlength\parindent{0pt} \usepackage{booktabs} \usepackage{colortbl} \arrayrulecolor{arrayrulecolor} \begin{document} \begin{tabular}{@{}ll@{}} \toprule \begin{minipage}[t]{0.47\columnwidth}\raggedright a \strut \end{minipage} & \begin{minipage}[t]{0.47\columnwidth}\raggedright b
\strut \end{minipage} \tabularnewline \begin{minipage}[t]{0.47\columnwidth}\raggedright \begin{tabularx} {\textwidth} {Xcrr} \bfseries Position & \bfseries Year 2020 & \bfseries Year 2021 \ \midrule \endhead \bottomrule \caption{Important Caption} \tabularnewline \endfoot \rowcolor{tableHighlightRowBackground} % this is another problem, this command applies the bg color to the complete outer row \bfseries A & { \bfseries 11,40 € } & { \bfseries 10,00 € } \ \end{tabularx} \strut \end{minipage} & \begin{minipage}[t]{0.47\columnwidth}\raggedright d \strut \end{minipage}\tabularnewline \bottomrule \end{tabular}

\end{document}

The desired output would be a caption below the inner tabularx (inside the outer tabular).

See this html-counterpart: enter image description here

My framework is quite flexible, there is just one invariant: The inner tabularx should be displayed as full-width table with a table caption and number.

olidem
  • 138
  • Please make your example compilable. Currently the documentclass, the booktabs and colortbl packages as well as the definition of the color tableHighlightRowBackground are missing. – leandriis Jul 20 '20 at 12:31
  • 1
    Please add some background information on why exactly you want to place a longtable inside of a minipage or a tabular. Neither of these two can break across pages. – leandriis Jul 20 '20 at 12:33
  • 1
    Please also clarify the desired output. In the question you mention table and caption being displayed side-by-side. However, I can't see that in teh code you provided. Wouldn't it be easier to use the sidenotes or floatrow package in order to place the caption side-by-side with its table? – leandriis Jul 20 '20 at 12:36
  • I edited my original question – olidem Jul 20 '20 at 12:54
  • Like that? https://i.stack.imgur.com/FQJlR.png What do a, b and d stand for? – leandriis Jul 20 '20 at 13:15
  • I think yes, your image seem to be what I am searching for. a b and c are dummy texts. Most of the time, I would have an outer table with one row and each column carrying an inner tabularx (to save space). – olidem Jul 20 '20 at 13:33
  • I have added an answer. However, I am still unsure of why you use nested tabulars here. Probably side by side minipages without the outer tabular could also work here? – leandriis Jul 20 '20 at 14:41

1 Answers1

1

Here is my suggestion based on some assumptions on the desired output. I have not changed the inner tabular with respect to avoiding the repetition of \bfseries.

enter image description here

\documentclass{article}
%\usepackage{ltablex}
%\keepXColumns
\usepackage{tabularx}
\usepackage{multirow}
\usepackage{caption}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\definecolor{tableHighlightRowBackground}{rgb}{0.8,0.8,0.8}
\begin{document}
\begin{tabular}{@{}p{0.9\textwidth}l@{}}
\toprule
a & b
\\
        {\begin{tabularx} {0.9\textwidth} {Xcrr}
        \bfseries
        Position & \bfseries Year 2020 & \bfseries Year 2021 \\
        \midrule
        \rowcolor{tableHighlightRowBackground} % this is another problem, this command applies the bg color to the complete outer row
        \bfseries A & { \bfseries 11,40 € } & { \bfseries 10,00 € } \\
        \bottomrule
        \end{tabularx}}
        \captionof{table}{Important Caption}
& 
d \\
\bottomrule
\end{tabular}
\end{document}
leandriis
  • 62,593
  • Great. Concerning your bfseries-remark: is the repetition a problem? I did not manage to format a full line bold face without bfseries or textbf{...} – olidem Jul 20 '20 at 14:52
  • So if I use the inner part (tabularx with captionof afterwards) alone, where is the caption placed? does this work for multipage tables? – olidem Jul 20 '20 at 14:53
  • @olidem: The repetition itself is not necessarily a problem but avoiding it shortens the code and makes your life easier ifyou decide you don't want the bold text. Regarding the inner tabularx: What to do entirely depends on the desired behaviour: Do you want your table to float? If so, use the table environment with \caption, not \captionof and tabularx inside. If not, stick with \captionof and tabularx. In order to make sure caption and table stay on the same page, surround both with a minipage (or center environment if you want to horizontally center the table on the page). – leandriis Jul 20 '20 at 15:00
  • Regarding multi page tables: For them you definitely need longtable (or a derived package such as xltabular.) Which package to use specifially again depends on the table, the requirements and the desired output. However, please keep in mind that a longtable can not be used inside of table or inside of a tabular. – leandriis Jul 20 '20 at 15:01
  • Yeah that's the point. My problem is that the inner table is the result of a placeholder which can occur (depending on user action) just in the document body directly or nested in a manually created outer table. But the placeholder is currently invariant under its placement. And it can become long. I saw a command to restore the original (non-multipage) version of tabularx here https://tex.stackexchange.com/a/134010/209995 but couldn't get it to work. This would be a solution as I could reset the environment in outer tabulars – olidem Jul 20 '20 at 15:14