3

Is it possible to add text on a coloured background or a cell to the middle of a line (similar to the image)? Thank you

\documentclass[11pt,a4paper]{article}
\usepackage[table]{xcolor}

\usepackage{graphicx}
\usepackage[resetfonts]{cmap}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{multicol}

\usepackage{multirow}

\textwidth 18cm
\textheight 26cm
\hoffset -1cm
\voffset -3cm
\oddsidemargin 0cm
\evensidemargin 0cm

\def\displayskip{\vskip6pt\relax}
\def\enddisplayskip{\vskip7pt\relax}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{bm}

\usepackage[flushleft]{threeparttable}

\usepackage{hhline}

\begin{document}

\begin{table}[h!]
\begingroup
\centering
\rowcolors{3}{white}{gray!20}
\def\arraystretch{1.4}
\begin{tabularx}{\textwidth}{|r|r|*{6}{>{\centering\arraybackslash}X|}l|}
\hline
          &      & \multicolumn{2}{c|}{\textbf{A}}  & \multicolumn{2}{c|}{\textbf{A}} & \multicolumn{2}{c|}{\textbf{B}}  & \\
\hhline{~~|------|~}
\multirow{-2}{*}{\textbf{A}}   & \multirow{-2}{*}{\textbf{B}}     & {\textbf{B}} & {\textbf{B}}  & \textbf{other} & \textbf{A}   & \textbf{A} & \textbf{A} & \multirow{-2}{*}{\textbf{B}} \\
\hline
     AAAAAA &   B & C  & D & E & F &  G &  -  & H \\
     AAAAAA &   B & C  & D & E & F &  G &  -  & H \\

\hline
     AAAAAA &   B & C  & D & E & F &  G &  -  & H \\
     AAAAAA &   B & C  & D & E & F &  G &  -  & H \\
\hline
     AAAAAA &   B & C  & D & E & F &  G &  -  & H \\
     AAAAAA &   B & C  & D & E & F &  G &  -  & H \\
\hline
\end{tabularx}
\par
\endgroup
\end{table}

\end{document}

enter image description here

After the advice:

enter image description here

enter image description here

2 Answers 2

5

With {NiceTabular} of nicematrix.

\documentclass[11pt,a4paper]{article}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage[resetfonts]{cmap}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{nicematrix,tikz}

\textwidth 18cm
\textheight 26cm
\hoffset -1cm
\voffset -3cm
\oddsidemargin 0cm
\evensidemargin 0cm
\def\displayskip{\vskip6pt\relax}
\def\enddisplayskip{\vskip7pt\relax}

\begin{document}

\begin{table}[h!]
\centering
\def\arraystretch{1.4}
\begin{NiceTabular}{rr*{6}{X[c]}l}[color-inside,vlines]
  \Hline
  \Block{2-1}{\textbf{A}} &  \Block{2-1}{\textbf{B}}    & \Block{1-2}{\textbf{A}}  && \Block{1-2}{\textbf{A}} &&
  \Block{1-2}{\textbf{B}}  && \Block{2-1}{\textbf{B}} \\
  \Hline
  &  & {\textbf{B}} & {\textbf{B}}  & \textbf{other} & \textbf{A}   & \textbf{A} & \textbf{A} & \\
  \Hline
  \rowcolors{gray!20}{}
       AAAAAA &   B & C  & D & E & F &  G &  -  & H \\
       AAAAAA &   B & C  & D & E & F &  G &  -  & H \\

  \Hline
       AAAAAA &   B & C  & D & E & F &  G &  -  & H \\
       AAAAAA &   B & C  & D & E & F &  G &  -  & H \\
  \Hline
       AAAAAA &   B & C  & D & E & F &  G &  -  & H \\
       AAAAAA &   B & C  & D & E & F &  G &  -  & H \\
  \Hline
\CodeAfter
  \begin{tikzpicture}
  \node at (3-|1) [fill=yellow!50,right] { \small text} ; 
  \node at (5-|1) [fill=yellow!50,right] { \small text} ; 
  \node at (7-|1) [fill=yellow!50,right] { \small text} ; 
  \end{tikzpicture}
\end{NiceTabular}
\end{table}

\end{document}

You need several compilations (because nicematrix uses PGF/TikZ nodes under the hood).

Output of the above code

6
  • Thank you. I am getting an error: Package nicematrix Error: Unknown key. for the line: \begin{NiceTabular}{rr*{6}{X[c]}l}[color-inside,vlines]
    – Elena Greg
    Commented Feb 26 at 10:02
  • 1
    You probably don't have the latest version of nicematrix. You should update your TeX installation (MiKTeX, TeXLive, etc.). Commented Feb 26 at 10:29
  • 1
    If you use Overleaf, you should upload the latest version of the file nicematrix.sty in the repertory of your Overleaf project. You can find that file on the SVN server of TeXLive: www.tug.org/svn/texlive/trunk/Master/texmf-dist/tex/latex/nicematrix/nicematrix.sty Commented Feb 26 at 10:30
  • Thank you. I added a figure to my question. Did you mean that, please?
    – Elena Greg
    Commented Feb 26 at 11:26
  • 1
    Yes. With the file nicematrix.sty uploaded, it should work. I have tried on Overleaf : here Commented Feb 26 at 11:49
2

With older version of NiceMatrix

enter image description here

MWE

\documentclass[11pt,a4paper]{article}
%\usepackage{xcolor, colortbl}
\usepackage{graphicx, color}
\usepackage[resetfonts]{cmap}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{nicematrix,tikz}

\textwidth 18cm
\textheight 26cm
\hoffset -1cm
\voffset -3cm
\oddsidemargin 0cm
\evensidemargin 0cm
\def\displayskip{\vskip6pt\relax}
\def\enddisplayskip{\vskip7pt\relax}

\begin{document}
    
    \begin{table}[h!]
        \centering
        \def\arraystretch{1.4}
        \begin{NiceTabular}{r*{7}{X[c]}l}
            \CodeBefore
            \rowcolors{2}{gray!15}{white!10}[respect-blocks]
            \Body
            \Hline
            \Block{2-1}{\textbf{A}} &  \Block{2-1}{\textbf{B}}    & \Block{1-2}{\textbf{A}}  && \Block{1-2}{\textbf{A}} &&
            \Block{1-2}{\textbf{B}}  && \Block{2-1}{\textbf{B}} \\
            \Hline
            &  & {\textbf{B}} & {\textbf{B}}  & \textbf{other} & \textbf{A}   & \textbf{A} & \textbf{A} & \\
            \Hline
        
            AAAAAA &   B & C  & D & E & F &  G &  -  & H \\
            AAAAAA &   B & C  & D & E & F &  G &  -  & H \\
            
            \Hline
            AAAAAA &   B & C  & D & E & F &  G &  -  & H \\
            AAAAAA &   B & C  & D & E & F &  G &  -  & H \\
            \Hline
            AAAAAA &   B & C  & D & E & F &  G &  -  & H \\
            AAAAAA &   B & C  & D & E & F &  G &  -  & H \\
            \Hline
            \CodeAfter
            \begin{tikzpicture}
                \node at (3-|1) [fill=yellow!50,right] { \small text} ; 
                \node at (5-|1) [fill=yellow!50,right] { \small text} ; 
                \node at (7-|1) [fill=yellow!50,right] { \small text} ; 
            \end{tikzpicture}
        \end{NiceTabular}
    \end{table}
    
\end{document}
1
  • Thank you. This works for me, but I need the alternating colours of rows and lines as in my question.
    – Elena Greg
    Commented Feb 26 at 11:48

You must log in to answer this question.

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