两个类似的 Tikz 图像间距/缩放/居中问题

两个类似的 Tikz 图像间距/缩放/居中问题

我首先要说明我最终想要得到的结果。我希望两幅图像并排。我希望点 G 在两张图片之间水平对齐,并且我希望两幅图像的比例相同。(左侧 GA 之间的距离与右侧 GA 之间的距离相同,但旋转角度不同)。最后,我希望左侧图像 G 位于页面左半部分的中心,右侧图像 G 位于页面右半部分的中心。

我的代码当前的工作方式是,两者之间的比例相同(因为它们都在同一个 tikzpicture 中),并且 G 点是水平的,因为它们在代码中都在 y=0 处。

我没有左右居中。因为左侧图片上的文本较多,所以它会将两张图片推到右侧。此外,这种方法有点失败,因为我手动指定了左侧和右侧 G 之间的距离。

如果我能够让 G 点的中点在页面上水平居中,我会对两者之间的这个固定距离感到满意。

我也尝试过做子图,但它们似乎会弄乱缩放比例,而且我仍然无法让点 G 在图形调整大小框中水平居中。

有人能帮助我让它正确居中而不需要过多地修改我的代码吗?

编辑:我想出了一个临时的解决方法,即在 tikzpicture 中画一条线,该线的左 G 和右 G 的中点居中,并且比图片更宽。但这似乎是一个粗略的解决方法。此外,还添加了更多序言和 \begin{document}。应该是一个可用的版本。

EDIT2:另外,如果您发现有其他方法可以改进我做事的方式,请提出建议。我对使用 Tikz 还很陌生,对很多细节还不太了解。

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{tikz}
\usepackage{caption,subcaption}

\newcommand\centerarc[6]{
    % \centerarc{format}{center_x}{center_y}{radius}{start angle}{end angle};
    \draw[#1] ({#2+#4*cos(#5 )},{#3+#4*sin(#5 )}) arc [radius=#4, start angle =#5, end angle=#6];
}

\newcommand\centerofmass{
    \tikz[radius=0.4em] {
        \fill (0,0) -- ++(0.4em,0) arc [start angle=0,end angle=90] -- ++(0,-0.8em) arc [start angle=270, end angle=180];
        \fill [color=white] (0,0) -- ++(0,0.4em) arc [start angle=90,end angle=180] -- ++(0.8em,0) arc [start angle=0, end angle=-90];
        \draw (0,0) circle;
    }
}

\begin{document}
\begin{figure}[]
    \centering
    \resizebox{!}{5in}{
        \begin{tikzpicture}[scale=0.5,>=stealth]
            \draw[line width=8pt, cap=round] (0,0)--(0,0.9);
            \draw[line width=8pt, cap=round] (0,0.9)--(0,5.1);
            \draw[line width=8pt, cap=round] (0,5.1)--(0,9.9);
            \draw[line width=8pt, cap=round] (0,9.9)--(0,18.1);
            \filldraw[fill=white] (0,0) circle (0.2);
            \filldraw[fill=white] (0,0.9) circle (0.2);
            \filldraw[fill=white] (0,5.1) circle (0.2);
            \filldraw[fill=white] (0,9.9) circle (0.2);
            \filldraw[fill=white] (0,18.1) circle (0.2);
            \node [] at (0,14) {\Large\centerofmass};
            \node [left] at (-1,0) {Ground Contact Point $\to$ G};
            \node [left] at (-1,0.9) {COR of Ankle $\to$ A};
            \node [right] at (1,5.1) {K$\gets$ COR of Knee};
            \node [left] at (-1,9.9) {COR of Hip $\to$ H};
            \node [left] at (-1,18.1) {Crown $\to$ C};
            \centerarc{}{0}{0.9}{0.7}{-90}{90};
            \node [right] at (0.7,0.9) {$\alpha$};
            \centerarc{}{0}{5.1}{0.7}{90}{270};
            \node [left] at (-0.7,5.1) {$\theta$};
            \centerarc{}{0}{9.9}{0.7}{-90}{90};
            \node [right] at (0.7,9.9) {$\beta$};
            \draw[line width=2pt, color=red, ->] (0,17)--(0,14);
            \node[anchor=west, color=red] at (0.2,17) {$F=2mg$};
            \draw[line width=2pt, color=red, ->] (0,-3)--(0,0);
            \node[anchor=west, color=red] at (0.2,-3) {$F=2mg$};
            \node[] at (0,-5) {(a)};
            \def\slide{12}
            \draw[line width=8pt, cap=round] (0+\slide,0)--(0+\slide,0.9);
            \draw[line width=8pt, cap=round] (0+\slide,0.9)--(2.068+\slide,3.655+0.9);
            \draw[line width=8pt, cap=round] (2.065+\slide,3.655+0.9)--(0+\slide,9.9-1.012);
            \draw[dashed] (0+\slide,0.9) -- (0+\slide,9.9-1.012);
            \draw[line width=8pt, cap=round] (0+\slide,9.9-1.012)--(0+\slide,18.1-1.012);
            \filldraw[fill=white] (0+\slide,0) circle (0.2);
            \filldraw[fill=white] (0+\slide,0.9) circle (0.2);
            \filldraw[fill=white] (2.065+\slide,3.655+0.9) circle (0.2);
            \filldraw[fill=white] (0+\slide,9.9-1.012) circle (0.2);
            \filldraw[fill=white] (0+\slide,18.1-1.012) circle (0.2);
            \node [] at (0+\slide,14-1.012) {\Large\centerofmass};
            \node [left] at (-1+\slide,0) {G};
            \node [left] at (-1+\slide,0.9) {A};
            \node [right] at (1+2.065+\slide,3.655+0.9) {K};
            \node [left] at (-1+\slide,9.9-1.012) {H};
            \node [left] at (-1+\slide,18.1-1.012) {C};
            \centerarc{}{0+\slide}{0.9}{0.7}{-90}{60.5};
            \node [right] at (0.7+\slide,0.9) {$\alpha$};
            \centerarc{}{2.068+\slide}{3.655+0.9}{0.7}{115}{240};
            \node [left] at (-0.7+2.068+\slide,3.655+0.9) {$\theta$};
            \centerarc{}{0+\slide}{9.9-1.012}{0.7}{90}{-65}
            \node [right] at (0.7+\slide,9.9-1.012) {$\beta$};
            \draw[line width=2pt, color=red, ->] (0+\slide,17-1.012)--(0+\slide,14-1.012);
            \node[anchor=west, color=red] at (0.2+\slide,17-1.012) {$F=2mg$};
            \draw[line width=2pt, color=red, ->] (0+\slide,-3)--(0+\slide,0);
            \node[anchor=west, color=red] at (0.2+\slide,-3) {$F=2mg$};
            \node[] at (0+\slide,-5) {(b)};
        \end{tikzpicture}
    }
    \caption{\label{fig:stick}(a) Vertical standing position model and notation convention for lab. (b) Points $G,A,H,C$ always stay co-linear during the course of the motion.}
\end{figure}
\end{document}

答案1

有用的东西:

  • scope环境。这允许单独设置诸如scale//red之类的属性thick
  • \paperwidth、、\textwidth72.27(这是您的边距)。
  • xshift或一般而言shift={(1,2)}
  • \useasboundingbox。这让 TeX 重新考虑 的边界框(因此大小)tikzpicture。它类似于smash或,clap但更加灵活。

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{tikz}
\usepackage{caption,subcaption}
\newcommand\centerarc[6]{
    \draw[#1]({#2+#4*cos(#5)},{#3+#4*sin(#5)})arc[radius=#4,start angle=#5,end angle=#6];}
\newcommand\centerofmass{
    \tikz[radius=0.4em]{
        \fill(0,0)--++(0.4em,0)arc[start angle=0,end angle=90]--++(0,-0.8em)arc[start angle=270,end angle=180];
        \fill[color=white](0,0)--++(0,0.4em)arc[start angle=90,end angle=180]--++(0.8em,0)arc[start angle=0,end angle=-90];\draw(0,0)circle;}}
\begin{document}
\begin{figure}
    \noindent
        \begin{tikzpicture}[>=stealth]
        \useasboundingbox(-.25\paperwidth+72.27,-5)rectangle(0,7);
        \begin{scope}[scale=0.5]
            \draw[line width=8pt, cap=round] (0,0)--(0,0.9);
            \draw[line width=8pt, cap=round] (0,0.9)--(0,5.1);
            \draw[line width=8pt, cap=round] (0,5.1)--(0,9.9);
            \draw[line width=8pt, cap=round] (0,9.9)--(0,18.1);
            \filldraw[fill=white] (0,0) circle (0.2);
            \filldraw[fill=white] (0,0.9) circle (0.2);
            \filldraw[fill=white] (0,5.1) circle (0.2);
            \filldraw[fill=white] (0,9.9) circle (0.2);
            \filldraw[fill=white] (0,18.1) circle (0.2);
            \node [] at (0,14) {\Large\centerofmass};
            \node [left] at (-1,0) {Ground Contact Point $\to$ G};
            \node [left] at (-1,0.9) {COR of Ankle $\to$ A};
            \node [right] at (1,5.1) {K$\gets$ COR of Knee};
            \node [left] at (-1,9.9) {COR of Hip $\to$ H};
            \node [left] at (-1,18.1) {Crown $\to$ C};
            \centerarc{}{0}{0.9}{0.7}{-90}{90};
            \node [right] at (0.7,0.9) {$\alpha$};
            \centerarc{}{0}{5.1}{0.7}{90}{270};
            \node [left] at (-0.7,5.1) {$\theta$};
            \centerarc{}{0}{9.9}{0.7}{-90}{90};
            \node [right] at (0.7,9.9) {$\beta$};
            \draw[line width=2pt, color=red, ->] (0,17)--(0,14);
            \node[anchor=west, color=red] at (0.2,17) {$F=2mg$};
            \draw[line width=2pt, color=red, ->] (0,-3)--(0,0);
            \node[anchor=west, color=red] at (0.2,-3) {$F=2mg$};
            \node[] at (0,-5) {(a)};
            \def\slide{12}
        \end{scope}
        \begin{scope}[scale=0.5,xshift=\paperwidth]
            \draw[line width=8pt, cap=round] (0,0)--(0,0.9);
            \draw[line width=8pt, cap=round] (0,0.9)--(2.068,3.655+0.9);
            \draw[line width=8pt, cap=round] (2.065,3.655+0.9)--(0,9.9-1.012);
            \draw[dashed] (0,0.9) -- (0,9.9-1.012);
            \draw[line width=8pt, cap=round] (0,9.9-1.012)--(0,18.1-1.012);
            \filldraw[fill=white] (0,0) circle (0.2);
            \filldraw[fill=white] (0,0.9) circle (0.2);
            \filldraw[fill=white] (2.065,3.655+0.9) circle (0.2);
            \filldraw[fill=white] (0,9.9-1.012) circle (0.2);
            \filldraw[fill=white] (0,18.1-1.012) circle (0.2);
            \node [] at (0,14-1.012) {\Large\centerofmass};
            \node [left] at (-1,0) {G};
            \node [left] at (-1,0.9) {A};
            \node [right] at (1+2.065,3.655+0.9) {K};
            \node [left] at (-1,9.9-1.012) {H};
            \node [left] at (-1,18.1-1.012) {C};
            \centerarc{}{0}{0.9}{0.7}{-90}{60.5};
            \node [right] at (0.7,0.9) {$\alpha$};
            \centerarc{}{2.068}{3.655+0.9}{0.7}{115}{240};
            \node [left] at (-0.7+2.068,3.655+0.9) {$\theta$};
            \centerarc{}{0}{9.9-1.012}{0.7}{90}{-65}
            \node [right] at (0.7,9.9-1.012) {$\beta$};
            \draw[line width=2pt, color=red, ->] (0,17-1.012)--(0,14-1.012);
            \node[anchor=west, color=red] at (0.2,17-1.012) {$F=2mg$};
            \draw[line width=2pt, color=red, ->] (0,-3)--(0,0);
            \node[anchor=west, color=red] at (0.2,-3) {$F=2mg$};
            \node[] at (0,-5) {(b)};
        \end{scope}
        \end{tikzpicture}
    \caption{\label{fig:stick}(a) Vertical standing position model and notation convention for lab. (b) Points $G,A,H,C$ always stay co-linear during the course of the motion.}
\end{figure}
\end{document}

相关内容