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 have a longtable with some columns, all of fixed with (e.g. p{3cm}), so all rows are of the same known width. I want to add a textual watermark (like "New") behind some rows. Of course the rows can have different heights; the watermark should be horizontally and vertically centered. How can I achieve this?

share|improve this question
add comment

3 Answers

up vote 7 down vote accepted

Using Tikz, you can define the \NEWROW command as follows:

\newcommand{\NEWROW}{%
  \noalign{%
    \dimen0=\dimexpr\prevdepth+\ht\strutbox\relax%
    \begin{tikzpicture}[remember picture, overlay]%
      \pgfsetfillopacity{0.5}
      \node[draw,minimum height=\the\dimen0, minimum width=3cm+2\tabcolsep, rectangle, anchor=south west, color=red] at (-2.5\tabcolsep,0) {NEW};
    \end{tikzpicture}%
  }%
}

The height seams to be a little bit off on the lower rows. I just took the code from @egreg, I didn't know how to get the rowheight. The draw is on the node to show the box it creates so you can see the difference with the actual height. The example would look like this:

Tikz watermark

Edit: Obviously the difference in the single row column is due to the fact that I used caps in the watermark text, which are higher than the row dimensions. As long as you don't use a watermark that doesn't fit in the row, you will be fine.

Update: To rotate the text is not completely trivial. I don't know of any way to rotate just the text and not the accompanying box. If you turn off the draw option it won't show the box anymore (you probably don't want that anyway) so then you would say it's not a problem, we can just rotate by adding rotate=x to the options. This is a problem though, since this kind of rotation uses the anchor to rotate around. In our case this means the box is rotated around the bottom left corner, which means that the text will move away from the center. For this, TikZ has the rotate around option which takes an argument like {angle:(x,y)} and rotates the node angle degrees around (x,y). Unfortunately, this means we have to calculate the center and use that, this is not very hard though.

Alternatively, we could draw two nodes, one with an empty text and one with just the text in it, but not the same size (we have to calculate the placement of course, again not extremely difficult). The only real advantage this has is that it allows you to keep the box! Again, you probably don't care about the box :) (if you don't care about the box, use the first option, less nodes=more better!). The two methods are demonstrated in the following \NEWROW command, take your pick:

\newcommand{\NEWROW}{%
  \noalign{%
    \dimen0=\dimexpr\prevdepth+\ht\strutbox\relax%
    \begin{tikzpicture}[remember picture, overlay]%      
      \pgfsetfillopacity{0.5}
      \pgfmathsetlengthmacro{\mydim}{(3cm+2\tabcolsep)/2}
      \pgfmathsetlengthmacro{\myht}{height("NEW")/2}      
      \node[minimum height=\the\dimen0, minimum width=3cm+2\tabcolsep, rectangle, anchor=south west, color=blue, rotate around={10:(\mydim-2.5\tabcolsep,.5*\the\dimen0+\myht)}] (a) at (-2.5\tabcolsep,0) {NEW};
      \node[minimum height=\the\dimen0, rectangle, anchor=center, color=red, rotate=10] at (\mydim-2.5\tabcolsep,.5*\the\dimen0+\myht) {NEW};
    \end{tikzpicture}%
  }%
}

The placement of the text isn't completely equivalent, I suspect there is an inner sep running foul somewhere, or I have just made a mistake in the calculations... Regardless, it's close as makes no difference.

share|improve this answer
    
@whlt3 Very nice. I'm new to tikz, so how do I get the text rotated a bit (going from bottom left to top right)? –  Martin Schröder Nov 8 '11 at 16:21
1  
@MartinSchröder: See the updated answer, I didn't know if you would get notified just from the edit... –  Roelof Spijker Nov 8 '11 at 18:54
add comment

You can use the old trusted picture environment to position the text behind the row.

\documentclass{article}
\usepackage{xcolor}
\newcommand{\NEWROW}{%
  \noalign{\dimen0=\dimexpr\prevdepth+\ht\strutbox\relax
    \vbox to 0pt{\vskip-.5\dimen0
      \llap{\smash{$\vcenter{\hbox{\Huge NEW}}$}}\vss}}}
\def\watermark#1{%
\setlength{\unitlength}{1pt} 
\begin{picture}(0,0) 
\put(50,-35){\textcolor{red!50!white}{\Huge #1}}    
\end{picture} }
\begin{document}
\begin{tabular}{p{3cm}p{3cm}l}
\watermark{Revised}
abc def abc def abc def abc def abc def abc def abc def
abc def abc  def abc def abc def  abc def abc def abc def
&
abc def abc def abc def abc def abc def abc def abc def
abc def abc def abc def abc def abc def abc def abc def
& x\\
\hline
abc def abc def abc def abc def abc def abc def abc def
&
abc def abc def abc def abc def abc def abc def abc def
& x\\
\hline
abc def abc def 
&
abc def abc def
& x\\
\hline
abc def abc def abc def abc def abc def abc def abc def
&
abc def abc def abc def abc def abc def abc def abc def
& x
\end{tabular}
\end{document}

How to position the text exactly in the middle and center of the row can be automated, but I am not sure if it is worth the trouble. I am also not sure, if it looks right though; I tend to agree with @egreg that at the margin it may look better, especially with a right pointing arrow or small graphic.

enter image description here

It will look better if the background text is rotated a bit. For this you can use the rotatebox from the graphicx package

enter image description here

Just include the package and change the code as follows:

 \rotatebox[origin=c]{15}{\watermark{Revised}}
share|improve this answer
    
The watermark in your code is not vertically centered regardless of the rowheight. –  Martin Schröder Oct 17 '11 at 9:17
add comment

TeX doesn't know the width of a table until it has ended processing it. It's possible to put a watermark in the left margin of a tabular row:

\newcommand{\NEWROW}{%
  \noalign{\dimen0=\dimexpr\prevdepth+\ht\strutbox\relax
    \vbox to 0pt{\vskip-.5\dimen0
      \llap{\smash{$\vcenter{\hbox{\Huge NEW}}$}}\vss}}}

\begin{document}
\begin{tabular}{p{3cm}p{3cm}l}
abc def abc def abc def abc def abc def abc def abc def
abc def abc def abc def abc def abc def abc def abc def
&
abc def abc def abc def abc def abc def abc def abc def
abc def abc def abc def abc def abc def abc def abc def
& x\\
\NEWROW
\hline
abc def abc def abc def abc def abc def abc def abc def
&
abc def abc def abc def abc def abc def abc def abc def
& x\\
\NEWROW

\hline
abc def abc def 
&
abc def abc def
& x\\
\NEWROW
\hline
abc def abc def abc def abc def abc def abc def abc def
&
abc def abc def abc def abc def abc def abc def abc def
& x
\end{tabular}

By changing \llap{ to \hbox to 4cm{\hss the word "NEW" will be printed over the row, but horizontal centering is quite difficult to achieve.

Notice that \NEWROW must go after the \\ terminating the row.

enter image description here

If the width is known in advance, for instance the tabular is

\begin{tabular}{*{4}{p{3cm}}}

then you can define

\newcommand{\NEWROW}{%
  \noalign{\dimen0=\dimexpr\prevdepth+\ht\strutbox\relax
    \vbox to 0pt{\vskip-.5\dimen0
      \hbox to\dimexpr 12cm+8\tabcolsep{%
        \hss\smash{$\vcenter{\hbox{\Huge NEW}}$}\hss}\vss}}}

since 12cm+8\tabcolsep is the table width. Of course \hbox{\Huge NEW} can be changed into something more sensible, say a word in grey color.

share|improve this answer
2  
The width of the rows (and the table) is known; the unknown part is the height of the row. And I'd prefer a solution that is a bit more LaTeXy. –  Martin Schröder Oct 16 '11 at 20:45
    
@MartinSchröder See edit –  egreg Oct 16 '11 at 20:54
    
The text in your example is not vertically centered; if I remove the \Huge the text is top-aligned in single-lined rows. –  Martin Schröder Oct 16 '11 at 21:18
1  
@MartinSchröder It's kludgy, I know; but the vertical size of cells is not known before typesetting them, so putting the command before closing the row is out of question. How to place the watermark perhaps can be improved, but I don't see how, for the moment. For better centering adjust the figures in \vskip.33ex as you please: it depends on the height of the watermark, of course. –  egreg Oct 16 '11 at 21:46
1  
@MartinSchröder I guess that \NEWROW\noalign{\pagebreak} should work. –  egreg Nov 7 '11 at 17:39
show 7 more comments

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.