1

When using a tabular with textwidth (manually calculated) it turns out to be wider than the minipage created by \tcblisting:

\documentclass{article}

\newlength\mylenA
\newlength\invoicedescwidth

\usepackage{tcolorbox}
\tcbuselibrary{listings}

\usepackage{tabularx}
\begin{document}

    \begin{tcblisting}{title=tabular with manual column widths}
\settowidth\mylenA{%   
    \textsf{\textbf{Pos.Total sum}}}
\setlength\invoicedescwidth{%
    \dimexpr\textwidth-\mylenA\relax}       
\begin{tabular}
    {
    r
    p{\invoicedescwidth}
    r
    }%
    \textbf{Pos.} & \textbf{Other text} & \textbf{Total sum} \\
    \hline\\
\end{tabular}
    \end{tcblisting}


    \begin{tcblisting}{title=tabularx with textwidth}
\begin{tabularx}{\textwidth}
    {
    r
    X
    r
    }%
    \textbf{Pos.} & \textbf{Other text} & \textbf{Total sum} \\
    \hline\\
\end{tabularx}
    \end{tcblisting}
    
\end{document}

tcblisting with tabular

Tabularx work. Should both not be the same? I do not want to use tabularx since the multiple calls during the LaTeX build break calculations that are done in tabularx...

1

1 Answer 1

1

You're forgetting to subtract the \tabcolsep spaces and there are six of them. Also, since you're typesetting the table in serif type, using \textsf will choose the wrong width.

I'd use \linewidth in both instances, but it seems that \textwidth is correctly updated in the tcolorbox, so if you're typesetting in single column there's no real problem.

\documentclass{article}

\newlength\mylenA
\newlength\invoicedescwidth

\usepackage{tcolorbox}
\tcbuselibrary{listings}

\usepackage{tabularx}
\begin{document}

\begin{tcblisting}{title=tabular with manual column widths}
  \settowidth\mylenA{\textbf{Pos.Total sum}}
  \setlength\invoicedescwidth{%
    \dimexpr\linewidth-\mylenA-6\tabcolsep\relax
  }
  \begin{tabular}{
    r
    p{\invoicedescwidth}
    r
  }
  \textbf{Pos.} & \textbf{Other text} & \textbf{Total sum} \\
  \hline
  \end{tabular}
\end{tcblisting}


\begin{tcblisting}{title=tabularx with textwidth}
  \begin{tabularx}{\textwidth}{
    r
    X
    r
  }
  \textbf{Pos.} & \textbf{Other text} & \textbf{Total sum} \\
  \hline
  \end{tabularx}
\end{tcblisting}
    
\end{document}

enter image description here

5
  • Thanks! I actually had the -\tabcolsep included but had made a different mistake before that I fixed and forgot to check again with -\tabcolsep... But are there 6 and not 2? Even if they are counted double (for both both columns on either side of the gap) should there not only be 4? May 16 at 9:12
  • Would there also be a possibility to have this calculation independent of the font? This would not work if I now switch to serif font, right? May 16 at 9:14
  • 1
    @mrCarnivore How is LaTeX supposed to read your mind in deciding what font you want to use in the headers for the next table?
    – egreg
    May 16 at 9:30
  • Of course I do not want LaTeX to read my mind. However, there might be a setting to find out what the current font type is that will be used for the next table (assuming no more changes after the check)... May 16 at 10:09
  • @mrCarnivore \settowidth uses the current font. Additional font selections can be done in the argument. But then you need to use them also when typesetting the headers.
    – egreg
    May 16 at 10:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .