2

I want to create a custom environment für typesetting chord symbols in a table with automatic column width of the widest row and they must be separated by | to mimic a bar line.

Sometimes, one line in the table has less bars than other lines. In this case I want to merge remaining cells together, see my example.

This works okay with \multicolumn, but it doesn't when I use it in the first cell of a row, because it then somehow removes the very first column separator aka bar line.

If this behavior was the default in all columns I would rather create a command \gobblebars that just removes all remaining bar lines including the one just in front of the command. This would be the better solution because it is easier to use.

MWE:

\documentclass[a4paper]{article}
\usepackage{nicematrix}

\renewcommand{\familydefault}{\ttdefault}


\newenvironment{bars*}[1]{%
    \setlength{\tabcolsep}{3.5pt}%
    \begin{NiceTabular}[t]{*{#1}{@{|}l}}[columns-width=auto]%
}{%
    \end{NiceTabular}%
}

\newcommand{\mergebars}[2]{\multicolumn{#1}{@{}l}{#2 \phantom{X}}}
\newcommand{\gobblebars}[1]{\multicolumn{#1}{@{}l}{}}


\begin{document}

\begin{bars*}{4}
    B & E & F\# & G\#m \\
    B & \mergebars{3}{E F\#} \\
    \mergebars{4}{B} \\
    B & \gobblebars{3} \\
\end{bars*}

\end{document}

enter image description here

Line 1: This is just a normal row, nothing special

Line 2: \multicolumn works in columns >1

Line 3: \multicolumn removes very first column separator aka bar line which is not what I want

Line 4: I would rather have a command \gobblebars that behaves like a \multicolumn does in a first column and removes the column separator just in front of it, but it doesn't

Question:

Is it possible to create a command \gobblebars as described? If not, how can I use \multicolumn to not remove the very first column separator if it is called in the first column?

I cannot use | directly in the cell contents, because I use it as an altered active character within the song environment of the leadsheets package.

1 Answer 1

1

EDIT

In fact, in your definition of {bars*}, you should probably replace @{|}by@{>}>{|}(it's not equivalent because@{...}puts its content in the *previous* cell ---except for the first cell of the row of course--- whereas>{...}` puts its content in the next cell.

\documentclass[a4paper]{article}
\usepackage{nicematrix}

\renewcommand{\familydefault}{\ttdefault}

\newenvironment{bars*}[1]{%
    \setlength{\tabcolsep}{3.5pt}%
    \begin{NiceTabular}[t]{*{#1}{@{}>{|}l}}[columns-width=auto]%
}{%
    \end{NiceTabular}%
}

\newcommand{\mergebars}[2]
  { \multicolumn{#1}{@{|}l}{#2 \phantom{X}} }

\newcommand{\gobblebars}[1]{\multicolumn{#1}{@{}l}{}}

\begin{document}

\begin{bars*}{4}
    B & E & F\# & G\#m \\
    B & \mergebars{3}{E F\#} \\
    \mergebars{4}{B} \\
    B & \gobblebars{3} \\
\end{bars*}

\end{document}

Output of the above code


Here is a modification of your command \mergebars which behaves as you wish.

\documentclass[a4paper]{article}
\usepackage{nicematrix}

\renewcommand{\familydefault}{\ttdefault}

\newenvironment{bars*}[1]{%
    \setlength{\tabcolsep}{3.5pt}%
    \begin{NiceTabular}[t]{*{#1}{@{|}l}}[columns-width=auto]%
}{%
    \end{NiceTabular}%
}

\ExplSyntaxOn
\makeatletter
\newcommand{\mergebars}[2]
  { \multicolumn{#1}{@{\int_compare:nNnT \c@jCol = 0 {|}}l}{#2 \phantom{X}} }
\makeatother
\ExplSyntaxOff

\newcommand{\gobblebars}[1]{\multicolumn{#1}{@{}l}{}}

\begin{document}

\begin{bars*}{4}
    B & E & F\# & G\#m \\
    B & \mergebars{3}{E F\#} \\
    \mergebars{4}{B} \\
    B & \gobblebars{3} \\
\end{bars*}

\end{document}

Remark

The package nicematrix provides in its environments the LaTeX counter jCol (its TeX name is \c@jCol) for the number of the current column. However, it is incremented at the beginning of the cell and, in the \multicolumn, it is not yet incremented (that's why I have tested \c@jCol against 0 and not 1).

Output of the above code

1
  • Very nice solution, I didn't know of jCol (the NiceMatrix documentation is kind of confusing). Can you also come up with a way to implement the \gobblebars{x} command to remove the next x separators AND the one just in front of it as I described?
    – KSTNR
    Jul 15, 2023 at 11:06

You must log in to answer this question.

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