如何确保不同的图表(TikZ)以相同的方式对齐?

如何确保不同的图表(TikZ)以相同的方式对齐?

我进步了一点,画了几张图。通过 /begin{figure},我设法对齐了前两个图。但是,第三个图无法按我想要的方式对齐。显然,这是由于 y 标签 i_L 不同。我如何确保所有图都以相同的方式对齐,以便每个 DT、T/2... 都齐平?

% 以作者 Urs Zellweger 的名义开始 ([电子邮件保护]),s。http://texample.net/tikz/examples/phasor-diagram/ % 获得 tex.stackexchange.com 的帮助坐标系已全部设置好--现在如何填充?

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations}
\usetikzlibrary{arrows}


%\newcommand{\Gitter}[4]{ % Für die Gitterliniene, Änderungen hier betreffen ALLE Gitter %im Latex Dokument
%    \draw[very thin,color=gray] (#1,#3) grid (#2,#4);
%}

\newcommand{\Koordinatenkreuz}[6]{ % Für das Koordinatenkreuz, Änderungen hier betreffen ALLE Gitter im Latex Dokument
    \draw[->, >=latex, color=green!50!black] (#1,0) -- (#2,0) node[right] {#5};
    \draw[->, >=latex, color=green!50!black] (0,#3) -- (0,#4) node[left] {#6};
}

\newenvironment{Diagramm}[5]{ 
\begin{tikzpicture}
    \def\T{#1}
    \def\Thalf{#2}
    \def\DT{#3}
    \def\Label{#4}
    \def\Yrange{#5}

    %\Gitter{-.1}{10.1}{#5-0.1}{3.1}
    \Koordinatenkreuz{-.2}{10.3}{#5-0.2}{2.8}{$t$}{#4};
    \draw (#1,0) node[below]{$T$};
    \draw (#2,0) node[below]{$\frac{T}{2}$};
    \draw (#3,0) node[below]{$DT$};
}
{\end{tikzpicture}}


\begin{document}


% {T}{T/2}{DT}{y-Label}{negative y-range}
\begin{figure}
\centering
\begin{Diagramm}{8}{4}{2}{$U_{GS_{1,4}}$}{0}
\draw [thick,red] (0,2) --++ (2,0) --++ (0,-2) -- (8,0) --++(0,2) --++ (1,0);
\end{Diagramm}
\begin{Diagramm}{8}{4}{2}{$U_{GS_{2,3}}$}{0}
\draw [thick,red] (0,0) -- (4,0) --++ (0,2) --++ (2,0) --++(0,-2) -- (9,0);
\end{Diagramm}
\begin{Diagramm}{8}{4}{2}{$i_L$}{0}
\draw [thick,red] (0,0) -- (4,0) --++ (0,2) --++ (2,0) --++(0,-2) -- (9,0);
\end{Diagramm}


\end{figure}


\end{document}

答案1

你的绘图方法有一些缺点,例如,看看如果你在第一个图表中使用

\begin{Diagramm}{8}{4}{2}{$U_{GS_{1,4}}(abcdefg)$}{0}
\draw [thick,red] (0,2) --++ (2,0) --++ (0,-2) -- (8,0) --++(0,2) --++ (1,0);
\end{Diagramm}

(它突出于右文本边缘)。换句话说,如果图表标签不太宽,您的方法就会很有效。

让我们回到你的问题上。我看到了三个简单的解决方案:

  • 使用我下面针对您的问题的评论中的建议:

    \begin{figure}
\raggedleft
\begin{Diagramm}{8}{4}{2}{$U_{GS_{1,4}}(abcdefg)$}{0}
\draw [thick,red] (0,2) --++ (2,0) --++ (0,-2) -- (8,0) --++(0,2) --++ (1,0);
\end{Diagramm}
\begin{Diagramm}{8}{4}{2}{$U_{GS_{2,3}}$}{0}
\draw [thick,red] (0,0) -- (4,0) --++ (0,2) --++ (2,0) --++(0,-2) -- (9,0);
\end{Diagramm}
\begin{Diagramm}{8}{4}{2}{$i_L$}{0}
\draw [thick,red] (0,0) -- (4,0) --++ (0,2) --++ (2,0) --++(0,-2) -- (9,0);
\end{Diagramm}
    \end{figure}
  • 轴标签y包含\llap{...}

\Koordinatenkreuz{-.2}{10.3}{#5-0.2}{2.8}{$t$}{{\llap{#4}}};

并在figure环境中使用\centering。然而,现在将以中心图为中心,而不考虑y

  • 使用sloped节点选项旋转标签:

\newcommand{\Koordinatenkreuz}[6]{ % Für das Koordinatenkreuz, Änderungen hier betreffen ALLE Gitter im Latex Dokument
    \draw[->, >=latex, color=green!50!black] (#1,0) -- (#2,0) node[right] {#5};
    \draw[->, >=latex, color=green!50!black] (0,#3) -- 
                            node[minimum height=3ex,above,sloped] {#6}(0,#4); % <------
}

最后的解决结果是:

enter image description here

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows, decorations}
\newcommand{\Koordinatenkreuz}[6]{ % Für das Koordinatenkreuz, Änderungen hier betreffen ALLE Gitter im Latex Dokument
    \draw[->, >=latex, color=green!50!black] (#1,0) -- (#2,0) node[right] {#5};
    \draw[->, >=latex, color=green!50!black] (0,#3) --
                            node[minimum height=3ex,above,sloped] {#6}(0,#4); % <------
}

\newenvironment{Diagramm}[5]{
\begin{tikzpicture}%[trim axis left]
    \def\T{#1}
    \def\Thalf{#2}
    \def\DT{#3}
    \def\Label{#4}
    \def\Yrange{#5}
    %\Gitter{-.1}{10.1}{#5-0.1}{3.1}
    \Koordinatenkreuz{-.2}{10.3}{#5-0.2}{2.8}{$t$}{#4};
    \draw (#1,0) node[below]{$T$};
    \draw (#2,0) node[below]{$\frac{T}{2}$};
    \draw (#3,0) node[below]{$DT$};
}
{\end{tikzpicture}}

%-------------------------------- show page layout, only for test
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{figure}
\centering
\begin{Diagramm}{8}{4}{2}{$U_{GS_{1,4}}$}{0}
\draw [thick,red] (0,2) --++ (2,0) --++ (0,-2) -- (8,0) --++(0,2) --++ (1,0);
\end{Diagramm}
\begin{Diagramm}{8}{4}{2}{$U_{GS_{2,3}}$}{0}
\draw [thick,red] (0,0) -- (4,0) --++ (0,2) --++ (2,0) --++(0,-2) -- (9,0);
\end{Diagramm}
\begin{Diagramm}{8}{4}{2}{$i_L$}{0}
\draw [thick,red] (0,0) -- (4,0) --++ (0,2) --++ (2,0) --++(0,-2) -- (9,0);
\end{Diagramm}
    \end{figure}
\end{document}

第二种解决方案:

enter image description here

第一个解决方案:

enter image description here

enter image description here

我会考虑第一个或第三个解决方案。但是,我会研究使用以下方法的可能解决方案,而不是使用您绘制图表的方法pgfplots

\documentclass[11pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{arrows, decorations}

\pgfplotsset{Diagram/.style={
            axis lines = left,
    xmax=9.5,
    ymax=1.5,
    xlabel = {$t$},
    xlabel style={at={(1,0)},anchor=west},
    ylabel style={at={(0,1)},rotate={-90},anchor=north east},
    ytick=\empty,
    no marks,
    every axis plot post/.append style={very thick, red},
                    }% end of Diagram style
            }% end of pgfplotsset

%-------------------------------- show page layout, only for test
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{figure}
    \pgfplotsset{xticklabels={ , , DT, T/2, , T},
                 height=4cm, width=0.8\textwidth}
\centering
    \begin{tikzpicture}[trim axis left]
\begin{axis}[Diagram,
    ylabel = $U_{GS_{1,4}}$,
            ]
\addplot coordinates {(0,1)  (2,1) (2,0)  (8,0)  (8,1) (9,1)};
\end{axis}
\end{tikzpicture}

    \begin{tikzpicture}[trim axis left]
\begin{axis}[Diagram,
    ylabel = $U_{GS_{2,3}}$,
            ]
\addplot coordinates {(0,0)  (4,0)  (4,1)  (6,1) (6,0) (9,0)};
\end{axis}
\end{tikzpicture}

    \begin{tikzpicture}[trim axis left]
\begin{axis}[Diagram,
    ylabel = $i_L$,
            ]
\addplot coordinates {(0,0)  (4,0)  (4,1)  (6,1) (6,0) (9,0)};
\end{axis}
\end{tikzpicture}
    \end{figure}
\end{document}

enter image description here

附录: 另一个简单的解决方案是移动y图表内侧的轴标签:-):

enter image description here

可以使用“纯”语法绘制附加线(例如变量的平均值),tikz例如\draw[<line specification>] (0,0.35) -- (9,0.35):

上图的代码是:

\documentclass[11pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{arrows, decorations}

\pgfplotsset{Diagram/.style={
            axis lines = left,
    xmin=0, xmax=9.5,
    ymin=0, ymax=1.8,
    xlabel = {$t$},
    xlabel style={at={(1,0)},anchor=west},
    ylabel style={at={(0,1)},rotate={-90},anchor=north west},
    ytick=\empty,
    no marks,
    every axis plot post/.append style={very thick, red},
                    }% end of Diagram style
            }% end of pgfplotsset

%-------------------------------- show page layout, only for test
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{figure}
    \pgfplotsset{xticklabels={ , , DT, T/2, , T},
                 height=4cm, width=0.8\textwidth}
\centering
    \begin{tikzpicture}%[trim axis left]
\begin{axis}[Diagram,
    ylabel = $U_{GS_{1,4}}$,
            ]
\addplot coordinates {(0,1)  (2,1) (2,0)  (8,0)  (8,1) (9,1)};
\end{axis}
\end{tikzpicture}

    \begin{tikzpicture}%[trim axis left]
\begin{axis}[Diagram,
    ylabel = $U_{GS_{2,3}}$,
            ]
\addplot coordinates {(0,0)  (4,0)  (4,1)  (6,1) (6,0) (9,0)};
\end{axis}
\end{tikzpicture}

    \begin{tikzpicture}%[trim axis left]
\begin{axis}[Diagram,
    ylabel = $i_L$,
            ]
\addplot coordinates {(0,0)  (4,0)  (4,1)  (6,1) (6,0) (9,0)};
\draw[ultra thin] (0,0.35) --  ++ (9,0);
\end{axis}
\end{tikzpicture}
    \end{figure}
\end{document}

答案2

正确对齐图表的一种方法是将所有内容放在一个地方tikzpicture,并使用scope具有垂直偏移的环境。

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations}
\usetikzlibrary{arrows}

%\newcommand{\Gitter}[4]{ % Für die Gitterliniene, Änderungen hier betreffen ALLE Gitter %im Latex Dokument
%    \draw[very thin,color=gray] (#1,#3) grid (#2,#4);
%}

\newcommand{\Koordinatenkreuz}[6]{ % Für das Koordinatenkreuz, Änderungen hier betreffen ALLE Gitter im Latex Dokument
        \draw[->, >=latex, color=green!50!black] (#1,0) -- (#2,0) node[right] {#5};
        \draw[->, >=latex, color=green!50!black] (0,#3) -- (0,#4) node[left] {#6};
}

\newenvironment{Diagramm}[6][]{ 
\begin{scope}[#1]
        \def\T{#2}
        \def\Thalf{#3}
        \def\DT{#4}
        \def\Label{#5}
        \def\Yrange{#6}

        %\Gitter{-.1}{10.1}{#5-0.1}{3.1}
        \Koordinatenkreuz{-.2}{10.3}{#6-0.2}{2.8}{$t$}{#5};
        \draw (#2,0) node[below]{$T$};
        \draw (#3,0) node[below]{$\frac{T}{2}$};
        \draw (#4,0) node[below]{$DT$};
}
{\end{scope}}

\begin{document}

% {T}{T/2}{DT}{y-Label}{negative y-range}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{Diagramm}{8}{4}{2}{$U_{GS_{1,4}}$}{0}
\draw [thick,red] (0,2) --++ (2,0) --++ (0,-2) -- (8,0) --++(0,2) --++ (1,0);
\end{Diagramm}
\begin{Diagramm}[yshift=-4cm]{8}{4}{2}{$U_{GS_{2,3}}$}{0}
\draw [thick,red] (0,0) -- (4,0) --++ (0,2) --++ (2,0) --++(0,-2) -- (9,0);
\end{Diagramm}
\begin{Diagramm}[yshift=-8cm]{8}{4}{2}{$i_L$}{0}
\draw [thick,red] (0,0) -- (4,0) --++ (0,2) --++ (2,0) --++(0,-2) -- (9,0);
\end{Diagramm}
\end{tikzpicture}
\end{figure}

\end{document}

相关内容