7

I currently have set up a command that draws a box with a shadow and some text inside that box. I want it to have a minimum-width, but when I set it, the table is now aligned in the center of the box when the table is smaller than the boxes width. I'd like it to be right-aligned. Here's the current setup:

\documentclass{scrlttr2}
\usepackage{tikz}
\usepackage{color}

\usetikzlibrary{shadows}

\definecolor{myblue}{rgb}{0, 0.58, 0.85}

\newcommand*\invhead[1]{\sffamily{\color{myblue}\footnotesize{\textbf{#1}}}}

\tikzstyle{shadedbox} = [
  draw=black,
  shade,
  top color=white,
  bottom color=bottom,
  drop shadow={
    top color=black,
    bottom color=black,
    shadow xshift=2pt,
    shadow yshift=-2pt,
  },
  thin,
  rectangle,
  inner sep=2pt,
  inner ysep=2pt
]

\newcommand*\monobox[2]{%
  \begin{tikzpicture}
    \definecolor{top}{RGB}{250,250,250}
    \definecolor{bottom}{RGB}{235,235,235}
    \node [shadedbox, minimum width=3.1cm] (box) {
      \begin{tabular}{r}
        \invhead{\scriptsize #1}\\
        \footnotesize{#2}\\
      \end{tabular}
    }
  \end{tikzpicture}
}

\setkomavar{fromname}{TestCompany}
\setkomavar{fromaddress}{37 Test Road, Someplace\\Somewhere, 6020\\Australia}
\setkomavar{fromphone}{+61 123 1234 1234}
\setkomavar{fromemail}{accounts@testcompany.com}
\setkomavar{signature}{TestCompany}
\setkomavar{invoice}[Invoice number]{1234}
\setkomavar{date}[Issue Date]{\today}
\setkomavar{customer}[Account number]{1234567}

\begin{document}
\begin{letter}{%
  Mr Nick Sandford\\
  23 Test St\\
  Somewhere, Someplace\\
  Australia
}

\opening{Dear Nick Sandford,}
\monobox{Test Box}{\$0.00}
\closing{Regards}
\end{letter}
\end{document}

I call it like \monobox{This Bill}{\$20.00}. This is what it looks like when it is rendered:

boxes

Hopefully I'm not doing anything too wrong :).

David Carlisle
  • 757,742
slurms
  • 551
  • 1
    Welcome to TeX.sx! Please always add a full but minimal working example (MWE) that illustrates your problem. Especially with TikZ it is important to know which libraries you are using. Also I don't know middle right and the PGF/TikZ manual doesn't mentioned it either. – Martin Scharrer Nov 05 '11 at 10:08
  • @MartinScharrer Oh, whoops - that wasn't supposed to be there, it was me randomly trying combinations of words to hopefully align it. Needless to say, it didn't work. I'll add a minimal working example in a second. – slurms Nov 05 '11 at 10:15
  • @MartinScharrer: Hopefully the example outputs successfully now :) – slurms Nov 05 '11 at 10:33

1 Answers1

8

To right align text in a node you must set the text width and the the align options on it. In your example code you could modify the monobox command to look like this:

\newcommand*\monobox[2]{%
  \begin{tikzpicture}
    \definecolor{top}{RGB}{250,250,250}
    \definecolor{bottom}{RGB}{235,235,235}
    \node [shadedbox, minimum width=3.1cm, text width=3.1cm, align=right] (box) {
      \begin{tabular}{r}
        \invhead{\scriptsize #1}\\
        \footnotesize{#2}\\
      \end{tabular}
    }
  \end{tikzpicture}
}

The result:

right aligned node text

Update: after Martin and Peter's comments I modfied the code, this should work regardless of the width of the input

\newcommand*\monobox[2]{%
 \begin{tikzpicture}
    \definecolor{top}{RGB}{250,250,250}
    \definecolor{bottom}{RGB}{235,235,235}
    \pgfmathsetlengthmacro{\mylen}{max(width("#1"),3.1cm)}
    \node [shadedbox, minimum width=3.1cm, text width=\mylen, align=right] (box) {
      \begin{tabular}{r}
        \invhead{\scriptsize #1}\\
        \footnotesize{#2}\\
      \end{tabular}
    };
  \end{tikzpicture}
}

Remaining problem is that the text is set using invhead and scriptsize and for some reason I can't get TikZ to accept these in the width operation. The manual says "protecting" it with a \noexpand should do the trick, but it doesn't. Therefore too much space is allocated. Anybody have any idea how this can be fixed?

Edit: after Martin's proposal to use a box, why is this not working?

Extra bonus-edit: Martin explained on chat and in this answer (Thank you Martin :)): How can I use an hbox inside a TikZ environment for text dimension measurement?

\newcommand*\monobox[2]{%
  \newsavebox\mybox
  \sbox{\mybox}{%
    \begin{tabular}{r}
      \invhead{\scriptsize #1}\\
      \footnotesize{#2}\\
    \end{tabular}
  }
  \begin{tikzpicture}
    \definecolor{top}{RGB}{250,250,250}
    \definecolor{bottom}{RGB}{235,235,235}
    \pgfmathsetlengthmacro{\mywd}{max(3.1cm,\wd\mybox)}
    \node [shadedbox, minimum width=3.1cm, text width=\mywd, align=right] (box) {
      \usebox{\mybox}
    };
  \end{tikzpicture}
}

This produces a box of the correct size, using the \mywd macro ensures that the text is also right aligned if it's shorter than 3.1cm.

David Carlisle
  • 757,742
Roelof Spijker
  • 17,663
  • 5
  • 55
  • 63
  • I had the same idea, but this doesn't give a minimal width, but fixes it. If the table content is wider it will stick out from the box. – Martin Scharrer Nov 05 '11 at 10:45
  • That is correct, each box will be at least 3.1 cm in this case. We could make a macro that takes the text that is passed to monobox as an argument, determines its width and then uses the minimum of that width and 3.1 cm as the value for text width. – Roelof Spijker Nov 05 '11 at 10:48
  • I think this is sufficient, The boxes should be larger than the content every time. Thanks! – slurms Nov 05 '11 at 10:50
  • @wh1t3 Good solution. You could improve the user interface by giving the user an optional argument in monoblock to set the width. – yannisl Nov 05 '11 at 12:27
  • 1
    I belive that you are missing a ; following the } before \end{tikzpicture} – Peter Grill Nov 05 '11 at 18:38
  • @PeterGrill: Yes, excellent! So is the example in the OP's question. I have modified the code, it appears the "'s are also necessary. – Roelof Spijker Nov 05 '11 at 18:52
  • I would box the full tabular instead and use its width (\wd), then \usebox inside the node. – Martin Scharrer Nov 05 '11 at 19:43
  • If you want to provide more flexibility in the user interface see how I have provided optional parameters in new command for a height flexible rect – Peter Grill Nov 05 '11 at 19:56
  • @MartinScharrer: I tried it, but couldn't get it to work... The usebox in the node doesn't seem to produce the table. I will add what I did in the answer, perhaps you could tell me what is wrong? :)

    @PeterGrill: I just took the monobox command from the MWE the OP provided, judging by how he is using commands and TikZ styles, I assume he knows how to pass arguments to the TikZ options as well. Nice answer though, +1 :)

    – Roelof Spijker Nov 05 '11 at 20:21