以节点为参考,将 tikzpicture 与正文对齐

以节点为参考,将 tikzpicture 与正文对齐

在类似下面的示例中,我想水平调整图形,以便网格与周围的文本和 y 轴对齐,并且标签突出到边距中。请注意,x 轴的宽度恰好如此,因此\textwidth应该可以舒适地容纳。我可以用负数手动执行此操作\hspace,但这不是很精确,而且也不符合 TeXy 规范。

理想情况下,我想做的是在环境中定义一个点tikzpicture作为外部对齐的参考点。我可以尝试将 y 轴与文本对齐等等。我在 Tikz 手册中没有找到这样做的方法。

\documentclass[a4paper,onesided]{article}
\usepackage{tikz}
\usepackage{blindtext}

\begin{document}
\blindtext

\begin{figure}[h!]
\begin{tikzpicture}[y=0.5mm, x=0.333\textwidth]

\def\axissep{5mm}
\def\tickl{2mm}
\def\xlabsep{3mm}

% y-axis
\draw (-\axissep,0) -- (-\axissep,100);
% y-ticks
\foreach \x in {10,20,30,40,60,70,80,90} % avoids double inking on 0, 50 and 100 that becomes thicker
    \draw (-\axissep,\x) -- ++ (-\tickl,0);
% y-tickslabels
\foreach \x in {0,50,100}
    \draw (-\axissep,\x) -- ++ (-\tickl*2,0);
\foreach \x in {0,50,100}
    \node[anchor=east] at (-\axissep-\tickl*2,\x) {\x};

% x-axis
\draw (0,-\axissep) -- ++ (3,0);
\foreach \x in {0,1,...,3}
    \draw (\x,-\axissep) -- ++ (0,-\tickl);

% x-labels
\node[below] at (0,-\axissep-\tickl){1};
\node[below] at (1,-\axissep-\tickl){2};
\node[below] at (2,-\axissep-\tickl){3};
\node[below,align=center] at (3,-\axissep-\tickl){4};

% grid
\foreach \x in {0,10,...,100}
    \draw[very thin, color=gray] (0,\x) -- (3,\x);

\draw (0,10) -- (1,14) --(2,45) -- (3,23) ++ (1mm,0) node[anchor=west] {\tiny Some label};

\end{tikzpicture}
\end{figure}

\blindtext

\end{document}

在此处输入图片描述

答案1

只需在图的开头添加:

\useasboundingbox (0,-20) rectangle (3,100);

(-20 是为了给 x 轴上的刻度留出空间)

在此处输入图片描述

答案2

您可以添加trim lefttikzpicture

\documentclass[a4paper,oneside]{article}
\usepackage{tikz}
\usepackage{blindtext}

\begin{document}
\blindtext

\begin{figure}[h!]
\makebox[0pt][l]{\begin{tikzpicture}[y=0.5mm, x=0.333\textwidth,trim left]

\def\axissep{5mm}
\def\tickl{2mm}
\def\xlabsep{3mm}

% y-axis
\draw (-\axissep,0) -- (-\axissep,100);
% y-ticks
\foreach \x in {10,20,30,40,60,70,80,90} % avoids double inking on 0, 50 and 100 that becomes thicker
    \draw (-\axissep,\x) -- ++ (-\tickl,0);
% y-tickslabels
\foreach \x in {0,50,100}
    \draw (-\axissep,\x) -- ++ (-\tickl*2,0);
\foreach \x in {0,50,100}
    \node[anchor=east] at (-\axissep-\tickl*2,\x) {\x};

% x-axis
\draw (0,-\axissep) -- ++ (3,0);
\foreach \x in {0,1,...,3}
    \draw (\x,-\axissep) -- ++ (0,-\tickl);

% x-labels
\node[below] at (0,-\axissep-\tickl){1};
\node[below] at (1,-\axissep-\tickl){2};
\node[below] at (2,-\axissep-\tickl){3};
\node[below,align=center] at (3,-\axissep-\tickl){4};

% grid
\foreach \x in {0,10,...,100}
    \draw[very thin, color=gray] (0,\x) -- (3,\x);

\draw (0,10) -- (1,14) --(2,45) -- (3,23) ++ (1mm,0) node[anchor=west] {\tiny Some label};

\end{tikzpicture}}
\end{figure}

\blindtext

\end{document}

我还将 宽度 放置在tikzpicture内部以防止坏框过满,并将不正确的 更改为(尽管这是 的默认值)。\makebox0ptonesidedonesidearticle

在此处输入图片描述

相关内容