# How to emulate some options of \node from TikZ

Code

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \a in {0,45,...,315}%
{\node[line width = 0pt,
rotate     = \a,
anchor     = west,
inner sep  = 0pt,
opacity    = .5]{\rule{.5cm}{.1cm}};}%
\end{tikzpicture}
\end{document}


I would to get the same result with \rotatebox and \put but other interesting methods are allowed without TikZ.

Some explanations of the first code :

1. anchor = west is the most important if we want to place the rules at the same point
2. inner sep = 0pt to remove white space around the rule (> 0pt -> pretty pictures)
3. line width = 0p subtlety! without this a small white point appears at the center
4. opacity = .5 only to watch how the rules are placed.

First try

\documentclass{article}
\usepackage{tikz} % I keep TikZ because foreach is useful
\begin{document}

\begin{picture}(0,0)
\foreach \a in {0,45,...,315}%
{\put(0,0){\rotatebox[origin=l]{\a}{\mbox{\rule{.5cm}{.1cm}}}}}%
\end{picture}
\end{document}


It's normal because the boxes are misplaced, we can see why if we remove put:

I'm a serious guy so I found this question and some good and useful answers so ...

Second try

\documentclass{article}
\usepackage{tikz}
\makeatletter
\newcommand{\somespecialrotate}[3][]{%
\begingroup
\sbox\@tempboxa{\rotatebox[#1]{#2}{#3}}%
\@tempdima=-\wd\@tempboxa
\advance\@tempdima by 0.09cm  % this is one of the problem
\mbox{\hskip\@tempdima\usebox\@tempboxa}%
\endgroup}%
\makeatother
\begin{document}
\begin{picture}(0,0)
\foreach \a in {-90,-45,...,90}%
{\put(0,0){\rotatebox[origin=l]{\a}{\mbox{\rule{.5cm}{.1cm}}}}}%
\put(0,0){\somespecialrotate[origin=l]{180}{\mbox{\rule{.5cm}{.1cm}}}}%
\put(0,0){\somespecialrotate[origin=l]{135}{\mbox{\rule{.5cm}{.1cm}}}}%
\put(0,0){\somespecialrotate[origin=l]{225}{\mbox{\rule{.5cm}{.1cm}}}}%
\end{picture}
\end{document}


This looks fine but \advance\@tempdima by 0.09cm is not serious and if I compare with the first code

\documentclass{article}
\usepackage{tikz}
\makeatletter
\newcommand{\somespecialrotate}[3][]{%
\begingroup
\sbox\@tempboxa{\rotatebox[#1]{#2}{#3}}%
\@tempdima=-\wd\@tempboxa
\advance\@tempdima by 0.09cm  % this is one of the problem
\mbox{\hskip\@tempdima\usebox\@tempboxa}%
\endgroup}%
\makeatother
\begin{document}
\begin{picture}(0,0)
\foreach \a in {-90,-45,...,90}%
{\put(0,0){\rotatebox[origin=l]{\a}{\mbox{\rule{.5cm}{.1cm}}}}}%
\put(0,0){\somespecialrotate[origin=l]{180}{\mbox{\rule{.5cm}{.1cm}}}}%
\put(0,0){\somespecialrotate[origin=l]{135}{\mbox{\rule{.5cm}{.1cm}}}}%
\put(0,0){\somespecialrotate[origin=l]{225}{\mbox{\rule{.5cm}{.1cm}}}}%
\end{picture}%
\color{red}%
\begin{tikzpicture} [overlay]
\foreach \a in {0,45,...,315}%
{\node[rotate=\a,anchor=west,inner sep=0pt,opacity=.5]{\rule{.5cm}{.1cm}};}%
\end{tikzpicture}
\end{document}


My question How to get the same picture with putand rotatebox?

-
pgffor package is the one responsible for the \foreach goodies. So you can skip TikZ and load only pgffor package. –  percusse Feb 28 '12 at 10:29
@percusse I agree with you and you are right. I know but I needed TikZ in the first and the last pictures so I keep TikZ for all the examples. But TikZ is not really the problem and I think the examples show how useful is this package. –  Alain Matthes Feb 28 '12 at 10:36
No problem at all. It was just a minor reminder for the record. –  percusse Feb 28 '12 at 12:57

Non-tikz attempt to a solution (but keeping the \foreach for convenience).

First of all it is better to utilize an empty framed box, as is easier to visualize.

For this I would use an \fbox with the fboxsep, providing some empty padding. We will also use the rotating package.

\documentclass{article}
\usepackage{pgffor} % I keep TikZ because foreach is useful
\usepackage{rotating}
\fboxsep10pt
\def\transparentbox{\fbox{\hbox to 3cm{}}}
\parindent0pt
\begin{document}
\foreach \a in {0,45,...,315}{%
\turnbox{\a}{\transparentbox}}%
\end{document}


-
But \usepackage{picture} is not useful here, isn't it ? –  Alain Matthes Feb 29 '12 at 9:02
I can forget \rotatebox:) and like percusse wrote in an initial comment \usepackage{pgffor} is enough –  Alain Matthes Feb 29 '12 at 9:14
@Altermundus Updated as per your comments and took all redundant packages out. –  Yiannis Lazarides Mar 1 '12 at 9:23
Thanks, your answer is very useful. I can now complete the examples for my new package pgfornament (it's like psvectorian but for pgf with more options). A last remark : you forgot to update the picture ! –  Alain Matthes Mar 1 '12 at 10:17
@Altermundus Good luck with the package, glad I could help. Updated the image as well. –  Yiannis Lazarides Mar 1 '12 at 10:39

Partial answer because I get some other problems. I try this code proposed by Martin \mbox{\hskip\dimexpr -.5\wd\bxb+.5\bxc\relax\usebox\bxc} but I get two problems. The first I introduce something wrong because the result is very bad but if I use the classical method

\@tempdima=\wd\bxb
\advance\@tempdima by -\wd\bxc  % this is one of the problem
\@tempdima=-0.5\@tempdima
\mbox{\hskip\@tempdima\usebox\bxc}%


Update with the help of Werner to use Martin's suggestion. dimexpr' is very useful. Now I need to reduce this code (three loops with three different macros.

\documentclass{article}
\usepackage{picture}
\usepackage{tikz}
\makeatletter
\newbox\bxb
\newbox\bxc
\newcommand{\somespecialrotate}[3][]{%
\begingroup
\sbox\bxb{#3}
\sbox\bxc{\rotatebox[#1]{#2}{#3}}%
\mbox{\hskip\dimexpr -.5\wd\bxb+.5\wd\bxc\relax\usebox\bxc}
\endgroup}%
\newcommand{\somespecialrotatei}[3][]{%
\begingroup
\sbox\bxb{#3}
\sbox\bxc{\rotatebox[#1]{#2}{#3}}%
\mbox{\hskip\dimexpr 0.5\wd\bxb-1.5\wd\bxc\relax\usebox\bxc}
\endgroup}%
\newcommand{\somespecialrotateii}[3][]{%
\begingroup
\sbox\bxb{#3}
\sbox\bxc{\rotatebox[#1]{#2}{#3}}%
\mbox{\hskip\dimexpr -0.5\wd\bxc\relax\usebox\bxc}
\endgroup}%
\makeatother
\begin{document}

\setlength{\fboxsep}{0pt}
\begin{picture}(0,0)
\foreach \a in {-45,0,45}%
{\put(0,0){%
\somespecialrotate[origin=l]{\a}{%
\tikz \node[minimum width =2cm,%
minimum height=4mm,%
inner sep =0pt,draw,%
line width=.4pt]{};%
}}}%
\foreach \a in {135,180,225}%
{\put(0,0){%
\somespecialrotatei[origin=l]{\a}{%
\tikz \node[minimum width =2cm,%
minimum height=4mm,%
inner sep =0pt,draw,%
line width=.4pt]{};%
}}}%
\foreach \a in {90,-90}%
{\put(0,0){%
\somespecialrotateii[origin=l]{\a}{%
\tikz \node[minimum width =2cm,%
minimum height=4mm,%
inner sep =0pt,draw,%
line width=.4pt]{};%
}}}%
\end{picture}%
\end{document}


-
Whenever you use a box in your \dimexpr, you need to reference a measure (like \wd or \ht or \dp). So, you would need \mbox{\hskip\dimexpr -.5\wd\bxb+.5\wd\bxc\relax\usebox\bxc}. –  Werner Feb 29 '12 at 5:51
@Werner Thanks I see the problem of syntax in the martin'code but I introduce a new one :( Now i would be happy if I can remove the three foreachand the three somespecialrotate . It's really ugly ! –  Alain Matthes Feb 29 '12 at 6:10
@Altermundus Please use \usepackage{picture} to use dimexpr and simplify the code. –  Yiannis Lazarides Feb 29 '12 at 7:49
@YiannisLazarides Yes I will update my answer but can you explain what are the advantages to use \usepackage{picture}`. I never used this package. –  Alain Matthes Feb 29 '12 at 8:19
@Altermundus It's a package by Heiko Oberdiek, it enables you to do calculations, but see my answer below first. I am not very familiar with TikZ let me know if my answer is what you are after. –  Yiannis Lazarides Feb 29 '12 at 8:53
show 1 more comment