[Tex/LaTex] Set width of \cmidrule or \cline

booktabstablestabularxxetex

I would like to prepare an income statement, so I found and modified a template for this on this site, giving the following MWE:

\documentclass[a4paper,12pt]{article}
        
\usepackage{textcomp}
\usepackage{tgschola}
\usepackage{tabularx}
\usepackage{icomma}
\usepackage{booktabs}

\newcolumntype{I}{@{}X<{\xdotfill}}
\newcolumntype{R}{>{\qq}r<{\qq}}

\makeatletter
        
\newcommand{\xdotfill}{\leavevmode\leaders\hb@xt@.44em{\hss.\hss}\hfill\hskip-\tabcolsep\kern\z@}
\makeatother
\newcommand\qq{\quad}
\newcommand\tablesec[1]{\multicolumn{1}{@{}l}{#1}&&\\}
\newcommand\tabletitle[1]{\multicolumn{1}{@{}c|}{\SEPx{2}#1}}
\newcommand\SEPx[1]{\vrule width 0pt height \dimexpr\fontcharht\font`A+2ex depth #1ex\relax}
\newcommand\SEP{\SEPx{0}}
\newcommand\?{\hphantom{0}}

\begin{document}

\renewcommand{\arraystretch}{1.2}
\noindent
\begin{tabularx}{\linewidth}{IRR}
\tablesec{Revenue:}
\qq Revenue 1 & & $5,00$ \\
\qq Revenue 2 & & $1247,50$ \\
\qq Revenue 3 & & $24,00$ \\

\tablesec{Expenses:}
\qq Cost 1 & $392,00$ \\
\qq Cost 2 & $206,82$ \\
\qq Cost 3 & $21,51$ \\
\cmidrule{2-2}
\qq Total cost & & $620,33$ \\
\cmidrule{3-3}
Net income & & $656,17$ \\
\cmidrule{3-3}\morecmidrules\cmidrule{3-3}
\end{tabularx}

\end{document}

The problem is here: the cmidrule under the totals is too wide, it fills the whole cell, I need it to be more narrow like this (see the red lines):
The cmidrule is too wide

Also, notice the red asterix: the booktabs-package which I'm using allows me to get a "double cline" in the last tabularx-rule, but this also causes a larger line spacing where it's used.

Maybe someone knows a solution 🙂

Thanks in advance,
Nino

Best Answer

I'd like to recommend that you load the siunitx package and employ its S column type to align the numbers in columns 2 and 3. Separately, use left- and right-trimming of the \cmidrules in column 2, and left-trimming of the \cmidrules in column 3. From an accounting class I took a long, long time ago, in a galaxy far away, I seem to remember that the rules should be as wide as the widest number in the respective columns. That's achieved by applying left- and right-trimming of the rules.

enter image description here

\documentclass[a4paper,12pt]{article}

\usepackage{tgschola}
\usepackage{booktabs}

\usepackage[output-decimal-marker={,},
            group-minimum-digits=4     % optional
           ]{siunitx}

\makeatletter
\newcommand{\xdotfill}{\leavevmode\leaders\hb@xt@.44em{\hss.\hss}\hfill\hskip-\tabcolsep\kern\z@}
\makeatother

\usepackage{tabularx}
\newcolumntype{I}{@{}X<{\xdotfill}}

\newcommand\qq{\quad}
\newcommand\tablesec[1]{\multicolumn{1}{@{}l}{#1}&&\\}
\newcommand\tabletitle[1]{\multicolumn{1}{@{}c|}{\SEPx{2}#1}}
\newcommand\SEPx[1]{\vrule width 0pt height \dimexpr\fontcharht\font`A+2ex depth #1ex\relax}
\newcommand\SEP{\SEPx{0}}
\newcommand\?{\hphantom{0}}

\begin{document}

\renewcommand{\arraystretch}{1.2}
\noindent
\begin{tabularx}{\linewidth}{I *{2}{S[table-format=4.2]} @{}}
\tablesec{Revenue}
\qq Revenue 1 &    5,00 \\
\qq Revenue 2 & 1247,50 \\
\qq Revenue 3 &   24,00 \\
\cmidrule(lr){2-2}
\qq Total income & & 1276.50 \\[1ex]
\tablesec{Expenses}
\qq Cost 1    & 392,00 \\
\qq Cost 2    & 206,82 \\
\qq Cost 3    &  21,51 \\
\cmidrule(lr){2-2}
\qq Total cost   & & 620,33 \\
\cmidrule(l){3-3}
Net income       & & 656,17 \\
\cmidrule(l){3-3}\morecmidrules\cmidrule(l){3-3}
\end{tabularx}

\end{document}