5

The number line command \nline from the makecell package inserts n cells with a pattern containing an incrementing number. I'd like to treat those as headers, styled with \theadfont and \theadalign. I looked at makecell's source, but I don't understand how patch it.

MWE

\documentclass[border=3pt]{standalone}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries\footnotesize}
\renewcommand\theadalign{cc}
\begin{document}
{\small\setcellgapes{8pt}\makegapedcells
  \begin{tabular}{@{}rllllll@{}}
    & \nline{6}\\\hline
  a & & & I'd like to center the numbers above. & & & \\
  b & & & (they should also be bold and footnotesize) & & & \\
  c & foo bar & & like thead & & & baz \\
\end{tabular}}
\end{document}
1
  • Styling with \theadalign would be difficult, but \theadfont is doable.
    – Werner
    Nov 15, 2018 at 0:11

2 Answers 2

4
\documentclass{article}

\usepackage{makecell}
\usepackage{xinttools}

\newcommand{\headalign}{\multicolumn{1}{c}}

\renewcommand\theadfont{\bfseries\footnotesize}

\begin{document}

\begin{tabular}{ r *{6}{l} }
    \xintFor* #1 in {\xintSeq{1}{6}}\do{&\headalign{\theadfont{#1}}}\\
    %       & \nline{6} \\ %(was for comparison)
  \hline
  a & & & I'd like to center the numbers above. & & & \\
  b & & & (they should also be bold and footnotesize) & & & \\
  c & foo bar & & like thead & & & baz
\end{tabular}

\end{document}

enter image description here

4
  • to create arithmetic sequence, for example \xintSeq[3]{2}{10} will expand to {2}{5}{8} which is then picked up by the \xintFor* loop.
    – user4686
    Nov 15, 2018 at 9:38
  • 1
    for more or less random numbers, input them explicitely: \xintFor* #1 in {{27}{-15}{9}{666}{3}{0}}\do{...} for example.
    – user4686
    Nov 15, 2018 at 9:41
  • nice. I did not know of this package. In fact I changed my mind about using makecell in general, because all of the functionality I need is supported in other packages in my project, or with readable and reasonable customization.
    – Bart
    Nov 15, 2018 at 9:42
  • @Bart admission of guilt: I authored the package. Its documentation is last chapter of xint.pdf (texdoc xint): Macros of the xinttools package. It is independent of rest of xint libraries.
    – user4686
    Nov 15, 2018 at 9:48
3

The following provides a partial solution to your question - applying \theadfont to each entry within \nline - via \nheadline:

enter image description here

\documentclass{article}

\usepackage{makecell,etoolbox}

\makeatletter
\newcommand{\nheadline}{%
  \patchcmd{\@@@nline}% <cmd>
    {& \Num}% <search>
    {& \theadfont\Num}% <replace>
    {}{}% <success><failure>
  \theadfont\nline
}
\makeatother

\renewcommand\theadfont{\bfseries}

\begin{document}

\begin{tabular}{ r *{6}{l} }
    & \nheadline{6} \\
    & \nline{6}     \\
  \hline
  a & & & I'd like to center the numbers above. & & & \\
  b & & & (they should also be bold and footnotesize) & & & \\
  c & foo bar & & like thead & & & baz \\
\end{tabular}

\end{document}

The problem with adding alignment is that you're required to use \multicolumn{1}{<align>} for every element, which has to be the first entry within the cell that it's used. The following example defines \createheadline{<num>} to create the appropriate headline combination that includes \multicolumn{1}{c} with each header element. You can use it outside of the tabular or (as I did) in the first cell that doesn't have anything else in it header-wise.

enter image description here

\documentclass{article}

\usepackage{makecell,multido}

\makeatletter

\newcommand{\headalign}{\multicolumn{1}{c}}
\newcommand{\createheadline}[1]{%
  \def\@@@headline{}%
  {%
  \let\headalign\relax
  \let\theadfont\relax
  \multido{\i=1+1}{#1}{%
    \edef\x{\noexpand\gdef\noexpand\@@@headline{\@@@headline & \headalign{\theadfont\i}}}\x%
  }%
  \expandafter\xdef\expandafter\@@@headline\expandafter{\expandafter\@gobble\@@@headline}%
  }%
}

\newcommand{\setheadline}{\@@@headline}

\makeatother

\renewcommand\theadfont{\bfseries}

\begin{document}

\begin{tabular}{ r *{6}{l} }
  \createheadline{6}  & \setheadline \\
    & \nline{6} \\
  \hline
  a & & & I'd like to center the numbers above. & & & \\
  b & & & (they should also be bold and footnotesize) & & & \\
  c & foo bar & & like thead & & & baz
\end{tabular}

\end{document}
1
  • Thanks for the effort, but I'm accepting @jfbu's answer because it is more readable, and I'm able to easily create sequences that don't start on one. I'ts not quite as described in makecell's docs (\nline[1][4]{3} ) but I have no hard requirements.
    – Bart
    Nov 15, 2018 at 9:37

You must log in to answer this question.

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