![如何原地旋转盒子](https://linux22.com/image/242743/%E5%A6%82%E4%BD%95%E5%8E%9F%E5%9C%B0%E6%97%8B%E8%BD%AC%E7%9B%92%E5%AD%90.png)
以下 LaTeX 代码说明了我的问题。是否可以在不执行否定操作的情况下获得我想要的结果\hskip
,因为我想要一个简单的通用解决方案是将盒子旋转到任意角度(中心原点)但将其保持在原位?
理想的解决方案是允许我这样做:
{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}
答案1
adjustbox
使用v0.8 中的可以轻松完成此操作2011/11/14
。它提供原始宽度,\Width
可用于将旋转的内容居中(其余部分在左侧和右侧重叠)或计算旋转内容应向左重叠的量,即您所说\hskip
的。当前宽度始终为\width
,因此lap=-.5\width+.5\Width
将使旋转的内容向左重叠。adjustbox
有关详细信息,请参阅手册。
使用旧版本的 ,如果您自己对内容进行装箱,则adjustbox
可以用 代替\Width
。\wd\bxb
如果您不想使用adjustbox
,而是自己动手,则需要对原始内容(\bxb
)和旋转后的内容(例如\bxc
)进行装箱,然后使用\mbox{\hskip\dimexpr -.5\wd\bxb+.5\wd\bxc\relax\usebox\bxc}
排版该框。
\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}
这里有一个graphicx
仅使用一个临时盒子寄存器的解决方案:
\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}
较低的部分:
这里的角度为 0,5,...,180:
答案2
使用\makebox
无需旋转即可获得相同的宽度:
\documentclass{article}
\usepackage{graphicx}
\usepackage{xcolor}
\newsavebox\bxa
\newsavebox\bxb
\makeatletter
\newcommand\somespecialrotate[3][]{%
\sbox\bxa{#3}\makebox[\wd\bxa]{\rotatebox[#1]{#2}{#3}}}
\makeatother
\def\RotateBox#1#2{\sbox{\bxa}{#2}\sbox\bxb{\rotatebox[origin=c]{#1}{#2}}%
\makebox[\wd\bxa]{\usebox\bxb}\kern\dimexpr(\wd\bxb-\wd\bxa)/2\relax}
\begin{document}
\huge
\color{red}\rule{1cm}{1cm}%
\rotatebox[origin=c]{45}{\color{black}\rule{1cm}{1cm}}This is what I do not want\par
\rule{1cm}{1cm}%
\RotateBox{45}{\color{black}\rule{1cm}{1cm}}This is what I DO want\par
\color{red}\rule{1cm}{1cm}%
\somespecialrotate[origin=c]{45}{\color{black}\rule{1cm}{1cm}}
\end{document}