# Problem with custom vertical-alignment and/or length of lines/rules in tabular environment

As a follow-up to my previous question: In my table / tabular,

• \cline{1-1} gives me the correct vertical position but not the right horizontal length .
• In contrast, \rule{\mycustomlength}{0.4pt} gives me the right (horizontal) length but not at the correct/desired vertical position.

How to solve? How to fine-tune? E.g. by adjusting

• either the lenght of \cline
• or the vertical position of \rule (but without introducing uneven line spacing further down ! )

Edit

• Do not want to change tabular's column widths.
• Do want to apply my custom length derived from a (randomly chosen) string, e.g. "blaaaaaa".

Here's a generic/random MWE:

\documentclass[a4paper]{article}

\newlength{\mycustomlength}
\settowidth\mycustomlength{blaaaaaaaaa}

%\usepackage{booktabs}

\begin{document}

\begin{tabular}{ccc}
aaaaaa &bbbb &ccc\\
\cline{1-1}
%%%%%
% The full syntax of the command is
% \cmidrule[ wd ]( trim ){a–b}
% \cmidrule[\mylength]{1-1}
dddd &e &f \\
\rule{\mycustomlength}{0.4pt}
\\
dddd &e &f \\
\end{tabular}
\end{document}

• Not sure what you're expecting with \rule{\mylength}{0.4pt}. It should be placed along the baseline of the context where it is called from. But you're not even calling it in your example. So not sure how to help you. – A.Ellett Jul 7 '13 at 21:16
• Also, I think you mean \setlength{\mylength}{0.4pt}. The way you've written it, you're setting \mylength to the length of 0.4pt as text, not as a measurement. – A.Ellett Jul 7 '13 at 21:21
• @A.Ellett You're right; corrected the MWE. Make more sense now ? – nutty about natty Jul 7 '13 at 21:25
• \rule... is not the same thing as an \hrule or \cline. The tabular environment sees the \rule... as something that inhabits a cell in your table. – A.Ellett Jul 7 '13 at 21:28

You can just use the definition of cline but patched to add a fixed length rule.

\documentclass[a4paper]{article}

\newlength{\mycustomlength}
\settowidth\mycustomlength{blaaaaaaaaa}

%\usepackage{booktabs}

\makeatletter
\def\mycline#1{\expandafter\my@cline\@cline#1\@nil}

\makeatother

\begin{document}

\begin{tabular}{ccc}
aaaaaa &bbbb &ccc\\
\cline{1-1}
dddd &e &f \\
\rule{\mycustomlength}{0.4pt}
\\
dddd &e &f \\
\mycline{1-1}
dddd &e &f \\
\end{tabular}

\end{document}

• How would you work a similar patch for \cmidrule? – A.Ellett Jul 7 '13 at 21:52
• @A.Ellett well cmidrule wiill be a leaders as well somewhere so you could just patch it the same way although probably I wouldn't bother and instead just adjust the vertical spacing in this one to be less like cline (which adds no space between the rows, so can over-print content) – David Carlisle Jul 7 '13 at 21:59
• @DavidCarlisle Tried adding \usepackage[table]{xcolor} but somehow this seem to be conflicting with the above customization... Any ideas how to reconcile 'em ? – nutty about natty Jul 8 '13 at 7:18
• @DavidCarlisle found a work-around / answer here: adding \PassOptionsToPackage{table}{xcolor} before \documentclass{article}. – nutty about natty Jul 8 '13 at 7:24
• I tried to use \arrayrulecolor{blue} (using your colortbl pkg) like so \arrayrulecolor{blue}\mycline{1-1} but that threw an error (undefined control sequence w.r.t. \arrayrulecolor). Any way to add colour to the custom 'mycline' ? – nutty about natty Jul 8 '13 at 7:43

You can make in your table a row of arbitrary height setting \arraystretch command to 0 before \begin{tabular}. However you will need to use \strut in every true row of your table to make its height right.

\documentclass[a4paper]{article}

\newlength{\mycustomlength}
\settowidth\mycustomlength{blaaaaaaaaa}

\begin{document}
\renewcommand{\arraystretch}{0}
\begin{tabular}{ccc}
\strut aaaaaa &bbbb &ccc\\
\hspace{-\arraycolsep}\rule{\mycustomlength}{0.4pt}\\
\strut blaaaaaaaaa &e &f
\end{tabular}
\end{document}


\hspace shifts line to the left border of the table. However if rule is longer than column, there would be a problem. To avoid it you can unite all columns in the fake row into one using multirow package.

\documentclass[a4paper]{article}

\usepackage{multirow}

\newlength{\mycustomlength}
\settowidth\mycustomlength{blaaaaaaaaa}

\begin{document}
\renewcommand{\arraystretch}{0}
\begin{tabular}{ccc}
\strut aaaaaa &bbbb &ccc\\
\multicolumn{3}{@{}l@}{\rule{\mycustomlength}{0.4pt}}\\
\strut dddd &e &f
\end{tabular}
\end{document}


Example images:

• Welcome to TeX.sx! I added the images for you. – Torbjørn T. Jul 8 '13 at 6:28

Of the two alternative routes to a solution to this problem, as outlined in my question above, David's solution addresses the former (adjusting the length of \cline) while egreg gave the key ingredient to the latter (adjusting the vertical position of \rule) elsewhere:

• to use fractions of \normalbaselineskip

and not of \baselineskip, because this parameter is zeroed in a tabular environment.

Since I want to end up with even spacing after the "rule intervention" (adding up the fractional line heights to give a whole number), this seems promising and doable, too.

Thanks egreg !

:-)

You could redefine the width of the cell. The \cline spans the length of the column and then some.

\documentclass[a4paper]{article}
\newlength{\mycustomlength}
\settowidth\mycustomlength{blaaaaaaaaa}
\begin{document}

\begin{tabular}{@{}c@{}cc}
aaaaaa & bbbb & ccc\\
\cline{1-1}
blaaaaaaaaa & & \\
\end{tabular}

\end{document}


Here \cline will be exactly the width of the widest column in your table. However, there is not buffer between columns. Contrast this with:

\begin{tabular}{ccc}
aaaaaa & bbbb & ccc\\
\cline{1-1}
blaaaaaaaaa & & \\
\end{tabular}


where you can see that the \cline is a bit wider than the longest text in the column.

If you want the buffer but you don't want the \cline to extend beyond the width of the widest text in the current column, then you can do something like:

\begin{tabular}{ccc}
aaaaaa & bbbb & ccc\\
\cmidrule(lr){1-1}
blaaaaaaaaa & & \\
\end{tabular}


Here's what these three options look like,

Notice that \cmidrule adds some extra vertical spacing that \cline doesn't.

• Not an option in my "real" (non-MWE) example. – nutty about natty Jul 7 '13 at 21:32