3

I have something like this:

\documentclass[]{article}

\usepackage{multirow}
\usepackage{booktabs}
\usepackage{listings}
\usepackage{xcolor}

\definecolor{corH}{HTML}{7030A0}
\definecolor{corE}{HTML}{FFC000}
\definecolor{corL}{HTML}{007523}

\lstdefinelanguage{StrucCompOut}{
    literate=
        {@}{{\colorbox{corH!20}{\makebox[1pt]{\strut\textcolor{corH}{H}}}}}1
        {\$}{{\colorbox{corH!10}{\makebox[1pt]{\strut\textcolor{corH!80}{h}}}}}1
        {?}{{\colorbox{corE!20}{\makebox[1pt]{\strut\textcolor{corE}{E}}}}}1
        {\&}{{\colorbox{corE!10}{\makebox[1pt]{\strut\textcolor{corE!80}{e}}}}}1
        {+}{{\colorbox{corL!20}{\makebox[1pt]{\strut\textcolor{corL}{L}}}}}1
        {*}{{\colorbox{corL!10}{\makebox[1pt]{\strut\textcolor{corL!80}{l}}}}}1,
    sensitive=true,
    numbers=none,
    breaklines=true,
    lineskip=0pt,
    basicstyle=\ttfamily,
    keepspaces=true
}
\lstset{
    language=StrucCompOut
}

\newcommand{\alnData}[1]{\lstinline[columns=fixed]$#1$}


\begin{document}

\begin{table}
\centering
    \begin{tabular}{ccc}
        \toprule
        \textbf{A} & \textbf{B} & \textbf{C} \\
        \midrule
        \multirow{2}[1]{*}{AAA} & \alnData{\$\$\$@@@???\&\&?******} & \multirow{2}[1]{*}{AAA} \\
                                & \alnData{aaaBBBCCCddCffffff} &  \\
                                & \alnData{   |||  |  |      } &  \\
        \multirow{2}[1]{*}{BBB} & \alnData{BBBBBBddCddCEEEEEE} & \multirow{2}[1]{*}{BBB} \\
                                & \alnData{@@@@@@\&\&?\&\&?++++++} &  \\
        \bottomrule
    \end{tabular}%
\end{table}

\end{document}

That is redering this:

enter image description here

But in my middle line with pipe chars I need to keep the spaces, i.e., I need something like this:

enter image description here

I already tried a lot of parameters and hacks that I read here, but I'm not able to make it work. I need to keep the spaces because the pipes must be aligned with my data. All the data will be inside a table and I have a lot of row sets with data similar of that. I think that inserting a space for literate would work, but I can't figure out how to do this.

2 Answers 2

3

You shouldn't absorb the tokens as argument, because this will compact spaces into one; instead, use a character that doesn't appear in the input, here '. You have also the advantage that you need no backslash in front of special characters.

\documentclass[]{article}

\usepackage{multirow}
\usepackage{booktabs}
\usepackage{listings}
\usepackage{xcolor}

\definecolor{corH}{HTML}{7030A0}
\definecolor{corE}{HTML}{FFC000}
\definecolor{corL}{HTML}{007523}

\lstdefinelanguage{StrucCompOut}{
    literate=
        {@}{{\colorbox{corH!20}{\makebox[1pt]{\strut\textcolor{corH}{H}}}}}1
        {\$}{{\colorbox{corH!10}{\makebox[1pt]{\strut\textcolor{corH!80}{h}}}}}1
        {?}{{\colorbox{corE!20}{\makebox[1pt]{\strut\textcolor{corE}{E}}}}}1
        {\&}{{\colorbox{corE!10}{\makebox[1pt]{\strut\textcolor{corE!80}{e}}}}}1
        {+}{{\colorbox{corL!20}{\makebox[1pt]{\strut\textcolor{corL}{L}}}}}1
        {*}{{\colorbox{corL!10}{\makebox[1pt]{\strut\textcolor{corL!80}{l}}}}}1,
    sensitive=true,
    numbers=none,
    breaklines=true,
    lineskip=0pt,
    basicstyle=\ttfamily,
    keepspaces=true
}
\lstset{
    language=StrucCompOut
}

\newcommand{\alnData}{\lstinline[columns=fixed]}


\begin{document}

\begin{table}
\centering
    \begin{tabular}{ccc}
        \toprule
        \textbf{A} & \textbf{B} & \textbf{C} \\
        \midrule
        \multirow{2}[1]{*}{AAA} & \alnData'$$$@@@???&&?******' & \multirow{2}[1]{*}{AAA} \\
                                & \alnData'aaaBBBCCCddCffffff' &  \\
                                & \alnData'   |||  |  |      ' &  \\
        \multirow{2}[1]{*}{BBB} & \alnData'BBBBBBddCddCEEEEEE' & \multirow{2}[1]{*}{BBB} \\
                                & \alnData'@@@@@@&&?&&?++++++' &  \\
        \bottomrule
    \end{tabular}
\end{table}

\end{document}

enter image description here

An implementation without listings:

\documentclass[]{article}

\usepackage{booktabs,multirow}
\usepackage{xcolor}
\usepackage{xparse}

\ExplSyntaxOn
% for setting up the special characters
\NewDocumentCommand{\definespecialchar}{mm}
 {
  \cs_new_protected:cpn { david \tl_to_str:n {#1} } { #2 }
 }
\NewDocumentCommand{\specialcolorbox}{mmm}
 { % #1=bg color; #2=text color; #3=char
  \group_begin:
  \setlength{\fboxsep}{0pt}
  \colorbox{#1}{\strut\color{#2}#3}
  \group_end:
 }

\NewDocumentCommand{\alnData}{v}
 {
  \david_alndata:n { #1 }
 }

\tl_new:N \__david_alnstring_tl

\cs_new_protected:Nn \david_alndata:n
 {
  \tl_set:Nn \__david_alnstring_tl { #1 }
  \regex_replace_all:nnN
   { [\@\$\?\&\+\*] }
   { \c{ david \0 } }
   \__david_alnstring_tl
  \texttt{ \__david_alnstring_tl }
 }
\ExplSyntaxOff

\definecolor{corH}{HTML}{7030A0}
\definecolor{corE}{HTML}{FFC000}
\definecolor{corL}{HTML}{007523}

\definespecialchar{@}{\specialcolorbox{corH!20}{corH}   {H}}
\definespecialchar{$}{\specialcolorbox{corH!10}{corH!80}{h}}
\definespecialchar{?}{\specialcolorbox{corE!20}{corE}   {E}}
\definespecialchar{&}{\specialcolorbox{corE!10}{corE!80}{e}}
\definespecialchar{+}{\specialcolorbox{corL!20}{corL}   {L}}
\definespecialchar{*}{\specialcolorbox{corL!10}{corL!80}{l}}


\begin{document}

\begin{table}
\centering
    \begin{tabular}{ccc}
        \toprule
        \textbf{A} & \textbf{B} & \textbf{C} \\
        \midrule
        \multirow{2}[1]{*}{AAA} & \alnData'$$$@@@???&&?******' & \multirow{2}[1]{*}{AAA} \\
                                & \alnData'aaaBBBCCCddCffffff' &  \\
                                & \alnData'   |||  |  |      ' &  \\
        \multirow{2}[1]{*}{BBB} & \alnData'BBBBBBddCddCEEEEEE' & \multirow{2}[1]{*}{BBB} \\
                                & \alnData'@@@@@@&&?&&?++++++' &  \\
        \bottomrule
    \end{tabular}%
\end{table}

\end{document}

enter image description here

2
  • Hi. I'm getting a "Undefined control sequence. \alnData'@@@'" error when trying to compile the version without listings. It would be more suitable for me, but I need to solve this error. Commented Jul 30, 2017 at 11:11
  • @davidbuzatto First of all, ensure your TeX sustem is up-to-date
    – egreg
    Commented Jul 30, 2017 at 12:28
1

Add first:

  • {w}{{\colorbox{corL!0}{\makebox[1pt]{\strut\textcolor{corL!0}{}}}}}1,

... to create a white colorbox. w is empty(without text).

and the second colorbox {/}

  • {/}{{\colorbox{corL!0}{\makebox[1pt]{\strut\textcolor{black!100}{|}}}}}1

... has a white background and a black |.

And second step: Replace \alnData{ ||| | | } with \alnData{www///ww/ww/wwwwww}.

enter image description here

MWE:

\documentclass[]{article}

\usepackage{multirow}
\usepackage{booktabs}
\usepackage{listings}
\usepackage{xcolor}

\definecolor{corH}{HTML}{7030A0}
\definecolor{corE}{HTML}{FFC000}
\definecolor{corL}{HTML}{007523}

\lstdefinelanguage{StrucCompOut}{
    literate=
        {@}{{\colorbox{corH!20}{\makebox[1pt]{\strut\textcolor{corH}{H}}}}}1
        {\$}{{\colorbox{corH!10}{\makebox[1pt]{\strut\textcolor{corH!80}{h}}}}}1
        {?}{{\colorbox{red!20}{\makebox[1pt]{\strut\textcolor{corE}{E}}}}}1
        {\&}{{\colorbox{corE!10}{\makebox[1pt]{\strut\textcolor{corE!80}{e}}}}}1
        {+}{{\colorbox{corL!20}{\makebox[1pt]{\strut\textcolor{corL}{L}}}}}1
        {*}{{\colorbox{corL!10}{\makebox[1pt]{\strut\textcolor{corL!80}{l}}}}}1
        {/}{{\colorbox{corL!0}{\makebox[1pt]{\strut\textcolor{black!100}{|}}}}}1
        {w}{{\colorbox{corL!0}{\makebox[1pt]{\strut\textcolor{corL!0}{|}}}}}1,
    sensitive=true,
    numbers=none,
    breaklines=true,
    lineskip=0pt,
    basicstyle=\ttfamily,
    keepspaces=true
}
\lstset{
    language=StrucCompOut
}

\newcommand{\alnData}[1]{{\lstinline[columns=fixed]$#1$}}



\begin{document}

\begin{table}
\centering
    \begin{tabular}{ccc}
        \toprule
        \textbf{A} & \textbf{B} & \textbf{C} \\
        \midrule
        \multirow{2}[1]{*}{AAA} & \alnData{\$\$\$@@@???\&\&?******} & \multirow{2}[1]{*}{AAA} \\
                                & \alnData{aaaBBBCCCddCffffff} &  \\
                                & \alnData{www///ww/ww/wwwwww} &  \\
        \multirow{2}[1]{*}{BBB} & \alnData{BBBBBBddCddCEEEEEE} & \multirow{2}[1]{*}{BBB} \\
                                & \alnData{@@@@@@\&\&?\&\&?++++++} &  \\
        \bottomrule
    \end{tabular}
\end{table}

\end{document}

You must log in to answer this question.

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