3

The inner table (that is the 'very long description') goes outside of the page limits:

\documentclass{article}
\usepackage[margin=3cm]{geometry}
\usepackage{array}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{tabularx}

\begin{document}

\newenvironment{messageWithProperties}[1]
    {
      \noindent
      \tabularx{\textwidth}{>{\bfseries}lX}
          \toprule
          \midrule
          header &
              $\left\{\tabular{>{\bfseries}l l >{\sffamily}p{\dimexpr\textwidth-12\tabcolsep-5\fboxsep-7.5cm\relax}}
                  \toprule
                  id & integer & desc \\
                  key & integer & desc \\
                  \bottomrule
              \endtabular\right\}$ \\[4ex]
          \midrule
          #1 &
              $\left\{\tabular{>{\bfseries}l l >{\sffamily}p{\dimexpr\textwidth-12\tabcolsep-5\fboxsep-7.5cm\relax}}
                  \toprule
    }
    {
                  \bottomrule
              \endtabular\right\}$ \\[4ex]
          \midrule
          \bottomrule
      \endtabularx
    }

\begin{messageWithProperties}{someVeryLongName}
    userId & string & desc \\
    propertyName & veryLongStringType & a very very long description of the field \\
\end{messageWithProperties}

\end{document}

How to force it to be inside?

It would be nice to limit its size to \textwidth - \widthof{#1} and also allow to fill all available space. How to do that?

  • Welcome to TeX.SE! How did you arrive at the adjustment factor 7.5cm? Incidentally, which page size do you use: A4, US Letter, or something else? – Mico Nov 18 '16 at 18:54
  • @Mico, it's from the tex.stackexchange.com/a/177078/118353 answer. More tables become too thin or too wide if these values are changed. The page size is A4. – Velkan Nov 21 '16 at 13:31
3

I'd use environ for avoiding nesting tabularx in a clumsy way:

\documentclass{article}
\usepackage[margin=3cm]{geometry}
\usepackage{array}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{tabularx,environ}

\newlength{\bracewidth}
\settowidth{\bracewidth}{$\left\{\vbox to 1in{}\right.\kern-\nulldelimiterspace$}

\newcommand{\innerMWP}[1]{%
  $\left\{\begin{tabularx}{\dimexpr\hsize-2\bracewidth}{ >{\bfseries}l l >{\sffamily}X }
  #1
  \end{tabularx}\right\}$%
}
\NewEnviron{messageWithProperties}[1]
  {\noindent
   \begin{tabularx}{\textwidth}{>{\bfseries}lX}
   \toprule
   \midrule
   header & \innerMWP{%
     \toprule
     id & integer & desc \\
     key & integer & desc \\
     \bottomrule
     }%
   \\[4ex]
   \midrule
   #1 & \innerMWP{\toprule\BODY\bottomrule} \\[4ex]
  \midrule
  \bottomrule
  \end{tabularx}%
}

\begin{document}

\begin{messageWithProperties}{someVeryLongName}
    userId & string & desc \\
    propertyName & veryLongStringType & a very very long description of the field \\
\end{messageWithProperties}

\end{document}

enter image description here

Without the useless braces:

\documentclass{article}
\usepackage[margin=3cm]{geometry}
\usepackage{array}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{tabularx,environ}

\newcommand{\innerMWP}[1]{%
  \begin{tabularx}{\hsize}{ >{\bfseries}l l >{\sffamily}X }
  #1
  \end{tabularx}%
}
\NewEnviron{messageWithProperties}[1]
  {\noindent
   \begin{tabularx}{\textwidth}{>{\bfseries}lX}
   \toprule\addlinespace
   header & \innerMWP{%
     \toprule
     id & integer & desc \\
     key & integer & desc \\
     \bottomrule
     }%
   \\\addlinespace
   \midrule
   \addlinespace
   #1 & \innerMWP{\toprule\BODY\bottomrule} \\
   \addlinespace
  \bottomrule
  \end{tabularx}%
}

\begin{document}

\begin{messageWithProperties}{someVeryLongName}
    userId & string & desc \\
    propertyName & veryLongStringType & a very very long description of the field \\
\end{messageWithProperties}

\end{document}

enter image description here

  • That additionally solves (hopefully; need to test on the whole doc) another problem: inability to compose the table from smaller commands. I've got a dozen of variations of the header and a structure, so had to copy-paste the newenvironment for each of them. P. S. braces are not useless. – Velkan Nov 25 '16 at 22:05
  • Great! You have solved a couple of issues I was wondering: 1) how to find the width of the braces 2) how to nest a tabularx within the main tabularx (I tried but without success). Moreover I see that \cellwidth is not necessary because \hsize is equivalent. – CarLaTeX Nov 26 '16 at 5:53
3

I don't know if this is a suitable solution but I've found here the way to have the cell width and I've used this width to set the width of the nested table columns:

\documentclass{article}
\usepackage[margin=3cm]{geometry}
\usepackage{array}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{tabularx}
% These code lines are from: https://tex.stackexchange.com/a/30673/101651
\makeatletter
\newcommand\cellwidth{\TX@col@width}
\makeatother

\begin{document}

\newenvironment{messageWithProperties}[1]
    {
      \noindent
      \tabularx{\textwidth}{>{\bfseries}lX}
          \toprule
          \midrule
          header &
              $\left\{\tabular{>{\bfseries}p{.24\cellwidth} p{.27\cellwidth} >{\sffamily}p{.34\cellwidth}}
                  \toprule
                  id & integer & desc \\
                  key & integer & desc \\
                  \bottomrule
              \endtabular\right\}$ \\[4ex]
          \midrule
          #1 &
              $\left\{\tabular{>{\bfseries}p{.24\cellwidth} p{.27\cellwidth} >{\sffamily}p{.34\cellwidth}}
                  \toprule
    }
    {
                  \bottomrule
              \endtabular\right\}$ \\[4ex]
          \midrule
          \bottomrule
      \endtabularx
    }

\begin{messageWithProperties}{someVeryLongName}
    userId & string & desc \\
    propertyName & veryLongStringType & a very very long description of the field \\
\end{messageWithProperties}

\end{document}

enter image description here

P.S. = I've never done a nested table before, nor created a new environment, hence I've answered as an exercise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.