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 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 :).

share|improve this question
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 5 '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 5 '11 at 10:15
    
@MartinScharrer: Hopefully the example outputs successfully now :) –  slurms Nov 5 '11 at 10:33
add comment

1 Answer 1

up vote 8 down vote accepted

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.

share|improve this answer
    
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 5 '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 5 '11 at 10:48
    
I think this is sufficient, The boxes should be larger than the content every time. Thanks! –  slurms Nov 5 '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. –  Yiannis Lazarides Nov 5 '11 at 12:27
1  
I belive that you are missing a ; following the } before \end{tikzpicture} –  Peter Grill Nov 5 '11 at 18:38
show 4 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.