[Tex/LaTex] How to rotate a box in-place

boxesrotating

The following LaTeX code illustrates my question. Is it possible to get what I want without doing the negative \hskip, as I want a simple generic solution which will rotate the box to any angle (center origin) but leave it 'in-place'?

An ideal solution would allow me to do this:

{a}\somespecialrotate[origin=c]{\anyangle}{b}

\documentclass{article}
\usepackage{graphicx}
\usepackage{xcolor}
\begin{document}
\newsavebox\bxa
\newsavebox\bxb
\huge
\savebox\bxa{\color{red}\rule{1cm}{1cm}}
\savebox\bxb{\color{black}\rule{1cm}{1cm}}
\usebox\bxa\rotatebox[origin=c]{45}{\usebox\bxb}This is what I do not want\par
\usebox\bxa\hskip-0.25cm\rotatebox[origin=c]{45}{\usebox\bxb}This is what I DO want
\end{document}

enter image description here

Best Answer

This can be easily done with adjustbox v0.8 from 2011/11/14. It provides the original width as \Width which can be used to center the rotated content around it (with the rest overlapping on the left and right) or to calculate the amount which the rotated content should overlap to the left, which is the \hskip you are talking about. The current width is always \width, so lap=-.5\width+.5\Width will make the rotated content lap over to the left. See the adjustbox manual for details.

With an older version of adjustbox you can substitute \Width with \wd\bxb if you box the content by yourself. If you don't want to use adjustbox, but rather do it yourself, you need to box both the original content (\bxb) and the rotated one (say \bxc) and then use \mbox{\hskip\dimexpr -.5\wd\bxb+.5\wd\bxc\relax\usebox\bxc} to typeset the box.

\documentclass{article}
\usepackage{adjustbox}[2011/11/14]
\usepackage{xcolor}
\begin{document}
\newsavebox\bxa
\newsavebox\bxb
\huge
\savebox\bxa{\color{red}\rule{1cm}{1cm}}
\savebox\bxb{\color{black}\rule{1cm}{1cm}}
\usebox\bxa\rotatebox[origin=c]{45}{\usebox\bxb}This is what I do not want\par
\usebox\bxa\hskip-0.25cm\rotatebox[origin=c]{45}{\usebox\bxb}This is what I DO want

\usebox\bxa\adjustbox{origin=c,angle=45,center=\Width}{\usebox\bxb}This is with the original width

\usebox\bxa\adjustbox{origin=c,angle=45,lap=-.5\width+.5\Width}{\usebox\bxb}This is with overlapping only at the left

% As macro:
\newcommand{\somespecialrotate}[2][]{\adjustbox{#1,angle={#2},lap=-.5\width+.5\Width}}

\usebox\bxa\somespecialrotate[origin=c]{15}{\usebox\bxb}This is some text\par
\usebox\bxa\somespecialrotate[origin=c]{25}{\usebox\bxb}This is some text\par
\usebox\bxa\somespecialrotate[origin=c]{35}{\usebox\bxb}This is some text\par
\usebox\bxa\somespecialrotate[origin=c]{45}{\usebox\bxb}This is some text\par
\usebox\bxa\somespecialrotate[origin=c]{55}{\usebox\bxb}This is some text\par
\usebox\bxa\somespecialrotate[origin=c]{65}{\usebox\bxb}This is some text\par
\usebox\bxa\somespecialrotate[origin=c]{75}{\usebox\bxb}This is some text\par
\usebox\bxa\somespecialrotate[origin=c]{85}{\usebox\bxb}This is some text\par
\usebox\bxa\somespecialrotate[origin=c]{95}{\usebox\bxb}This is some text\par
\end{document}

Here a graphicx-only solution which only uses one temporary box register:

\documentclass{article}
\usepackage{graphicx}
\usepackage{xcolor}

\makeatletter
\newcommand{\somespecialrotate}[3][]{%
    \begingroup
    \sbox\@tempboxa{#3}%
    \@tempdima=.5\wd\@tempboxa
    \sbox\@tempboxa{\rotatebox[#1]{#2}{\usebox\@tempboxa}}%
    \advance\@tempdima by -.5\wd\@tempboxa
    \mbox{\hskip\@tempdima\usebox\@tempboxa}%
    \endgroup
}
\makeatother

\begin{document}
\huge
{\color{red}\rule{1cm}{1cm}}\somespecialrotate[origin=c]{45}{\color{black}\rule{1cm}{1cm}}This is some text\par
{\color{red}\rule{1cm}{1cm}}\somespecialrotate[origin=c]{30}{\color{black}\rule{1cm}{1cm}}This is some text\par

\end{document}

Result

Lower part:

Result2

And here for angles from 0,5,...,180:

Related Question