Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I had a look at this post on Misaligned multicolumn but it doesn't really solve my problem. I have the code below which is supposed to produce a table with a blue line for the first row and red lines for the other two.

In the table below there should be a single blue line and a single contiguous red line below it, with the numbers all aligned.

Could someone guide me to what I'm doing wrong? I recognize the dire imprecations against vertical lines but in the final output this will present financial information and I thought it would be useful to clearly separate the inflows from the outflows with a colour coded side line.

If anyone can help with the Misplaced \omit error too that would be really helpful

Vertical lines supposed to be aligned

\documentclass{article}
\usepackage{pgfplotstable}

\def\pgfplotstableeach(#1-#2,#3-#4,#5){% columns 1-2 rows 3-4 input 5
  \xdef\ACCUM{}%
  \foreach \col in {#1,...,#2} {%
    \foreach \ro in {#3,...,#4} {%
    \toks0=\expandafter{\ACCUM}%
    \edef\temp{every row \ro\space column \col/.style}%
    \toks1={#5}%
    \xdef\ACCUM{\the\toks0 \temp={\the\toks1},}%
    }%
  }%
  \message{setting \meaning\ACCUM^^J}%
  \expandafter\pgfplotstableset\expandafter{\ACCUM}%
}

\begin{document}

\pgfplotstableread[row sep=\\,col sep=&,header=false]{%
1 & 2 & 3 & 4\\%
5 & 6 & 7 & 8\\%
9 & 10 & 11 & 12\\%
}\mytable

\pgfplotstableset{
  outline/.style={postproc cell content/.style={@cell content=\fbox{##1}}},
  vblue/.style={postproc cell content/.style={@cell content=\multicolumn{1}{!{\color{blue}\vrule width 2pt height 1ex}l}{##1}}},
  vred/.style= {postproc cell content/.style={@cell content=\multicolumn{1}{!{\color{red} \vrule width 2pt height 2ex}l}{##1}}}
}

\pgfplotstableeach(0-0,1-2,{vred})
\pgfplotstableeach(0-0,0-0,{vblue})

\pgfplotstabletypeset[
debug,
column name={},
every row 2 column 1/.style={outline},
every row 1 column 0/.style={vred},
every row no 0/.style={after row={\rule{0pt}{2em}}}
]\mytable


\end{document}
share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

The content is inserted into the cell too late for \multicolumn which must have no non-expandable tokens before it. So something like this:

enter image description here

\documentclass{article}
\usepackage{pgfplotstable}

\def\pgfplotstableeach(#1-#2,#3-#4,#5){% columns 1-2 rows 3-4 input 5
  \xdef\ACCUM{}%
  \foreach \col in {#1,...,#2} {%
    \foreach \ro in {#3,...,#4} {%
    \toks0=\expandafter{\ACCUM}%
    \edef\temp{every row \ro\space column \col/.style}%
    \toks1={#5}%
    \xdef\ACCUM{\the\toks0 \temp={\the\toks1},}%
    }%
  }%
  \message{setting \meaning\ACCUM^^J}%
  \expandafter\pgfplotstableset\expandafter{\ACCUM}%
}

\begin{document}

\pgfplotstableread[row sep=\\,col sep=&,header=false]{%
1 & 2 & 3 & 4\\%
5 & 6 & 7 & 8\\%
9 & 10 & 11 & 12\\%
}\mytable

\pgfplotstableset{
  outline/.style={postproc cell content/.style={@cell content=\fbox{##1}}},
  vblue/.style={postproc cell content/.style={@cell content={\color{blue}\vrule width 2pt height 1ex\relax}\,##1}},
  vred/.style= {postproc cell content/.style={@cell content={\color{red} \vrule width 2pt height 2ex\relax}\,##1}}
}

\pgfplotstableeach(0-0,1-2,{vred})
\pgfplotstableeach(0-0,0-0,{vblue})

\pgfplotstabletypeset[
debug,
column name={},
every row 2 column 1/.style={outline},
every row 1 column 0/.style={vred},
every row no 0/.style={after row={\rule{0pt}{2em}}}
]\mytable


\end{document}

If you want the lines to run together like this:

enter image description here

Just delete the height component of the rules:

  vblue/.style={postproc cell content/.style={@cell content={\color{blue}\vrule width 2pt\relax}\,##1}},
  vred/.style= {postproc cell content/.style={@cell content={\color{red} \vrule width 2pt\relax}\,##1}}
share|improve this answer
    
That's brilliant @DavidCarlisle thank you. For my education could you explain what the new code is doing? I don't quite understand why it works, especially the last backslash. Thanks. –  Tahnoon Pasha Oct 31 '12 at 21:30
1  
the \, is just a thin space to separate the rule you could replace it by \hspace{1cm} the {\color...} group before that is a coloured rule and ##1 is (apparently) the original cell contents which must go through two levels of macro definition internally hence requiring a doubled #. The main point is that it is too late to change the cell preamble with multicolumn so you need to put the vrule in the cell content and space it "by hand" to separate it from the digits in the cell. –  David Carlisle Oct 31 '12 at 21:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.