tikzpicture 左侧未说明的空间

tikzpicture 左侧未说明的空间

在思考另一个问题的解决方案时(在小页面顶部绘制居中对齐线,未提交),我发现了一个奇怪的现象。在左边大约tikzpicture2pt。这不是什么大问题,而且很容易解决,但有点奇怪。我%在每行末尾都添加了,以确保不会无意中添加空格。

\documentclass{article}

\usepackage[showframe]{geometry}
\usepackage{xcolor,tikz}

\usetikzlibrary{positioning}

\newsavebox{\mybox}

\NewDocumentEnvironment{myenvii}{m}{%
    \parindent0pt%
    \begin{lrbox}{\mybox}%
        \begin{minipage}[c][5cm][t]{10cm}% both minipage and varwidth work here
        \vspace*{3pt}%
}{%
        \end{minipage}%
    \end{lrbox}%
    \noindent
    %\kern-2pt%% --->>> fixes the problem, but why necessary?!?!?
    \begin{tikzpicture}%
        \node[draw=cyan,fill=yellow](R){\usebox{\mybox}};
        \draw[line width=1ex,cyan] (R.north west) -- (R.north east);
        \node[inner xsep=0pt,cyan,above=2pt of R.north west,anchor=south west] {#1};
    \end{tikzpicture}%
}

\begin{document}

\noindent Some text outside Ti\emph{k}Z.

\begin{myenvii}{Some title}%
Some text This is some more to see just what happens here and what happens if there is a lit of text to see.
\end{myenvii}

\noindent$\uparrow$ note the gap here, about 2pt.

\end{document}

在此处输入图片描述

答案1

问题是:

\draw[line width=1ex,cyan] (R.north west) -- (R.north east);

要查看问题,请尝试:

\draw[line width=1ex,cyan,line cap=rect] (R.north west) -- (R.north east);

这是一个解决方案:

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{xcolor,tikz}

\usetikzlibrary{positioning}

\newsavebox{\mybox}

\NewDocumentEnvironment{myenvii}{m}{%
  \parindent0pt%
  \begin{lrbox}{\mybox}%
    \begin{minipage}[c][5cm][t]{10cm}% both minipage and varwidth work here
      \vspace*{3pt}%
    }{%
    \end{minipage}%
  \end{lrbox}%
  \noindent%
  \begin{tikzpicture}[outer sep=0pt]%
    \node[draw=cyan,fill=yellow](R){\usebox{\mybox}};
    \fill[fill=cyan] (R.north west) rectangle ([yshift=1ex]R.north east);
    \node[inner xsep=0pt,cyan,above=1ex of R.north west,anchor=south west] {#1};
  \end{tikzpicture}%
}

\begin{document}

\noindent Some text outside Ti\emph{k}Z.

\begin{myenvii}{Some title}%
Some text This is some more to see just what happens here and what happens if there is a lit of text to see.
\end{myenvii}
\end{document}

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}%
  \begin{tikzpicture}[outer sep=0pt]%
    \node[draw=cyan,fill=yellow](R){%
      toto% \usebox{\mybox}%
    };
    \fill[fill=cyan] (R.north west) rectangle ([yshift=1ex]R.north east);
    \node[inner xsep=0pt,draw,cyan,above=2pt of R.north west,anchor=south west] {Title};
    \draw[red] (current bounding box.south west) rectangle (current bounding box.north east);
  \end{tikzpicture}%
\end{document}

答案2

tcolorbox您可以考虑在新环境中使用:

\documentclass{article}
\usepackage[showframe]{geometry} % in real document remove option "showframe" 
\usepackage{tcolorbox}
\tcbuselibrary{skins}

\newtcolorbox{mybox}[1]{%
enhanced,
title=#1,
fonttitle=\Large\bfseries,
coltitle=cyan,
attach boxed title to top left,
boxed title style={colframe=white,
                   colback=white,
                   },
%
before=\par\medskip\noindent,
    colframe=cyan,  colback=yellow!30,
    boxrule=1pt,    toprule=6pt,
    boxsep=3pt,
    arc=0mm,
    left=3pt,right=0mm,top=3pt,bottom=3pt,
%after=\noindent,   % <--- if you wish no indented text after box
}

\usepackage{lipsum}

\begin{document}
\lipsum[11]
\begin{mybox}{Some title}
\lipsum[1]
\end{mybox}
\lipsum[12]
\end{document}

在此处输入图片描述

答案3

另一个选项使用use as bounding box

  • 绘制应构成最终边界框的所有内容。在 OP 的示例中,有两个节点。
  • 用于\path[use as bounding box] ...;指定固定的边界框。
  • 添加不再影响边界框的 rest 路径。这是青色规则。
\documentclass{article}

\usepackage[showframe]{geometry}
\usepackage{xcolor,tikz}

\usetikzlibrary{positioning}

\newsavebox{\mybox}

\NewDocumentEnvironment{myenvii}{m}{%
    \parindent0pt%
    \begin{lrbox}{\mybox}%
        \begin{minipage}[c][5cm][t]{10cm}% both minipage and varwidth work here
        \vspace*{3pt}%
}{%
        \end{minipage}%
    \end{lrbox}%
    \noindent
    %\kern-2pt%% --->>> fixes the problem, but why necessary?!?!?
    \begin{tikzpicture}%
        \node[draw=cyan,fill=yellow](R){\usebox{\mybox}};
        \node[inner xsep=0pt,cyan,above=2pt of R.north west,anchor=south west]  (title) {#1};
        \path[use as bounding box] (title.north west) rectangle (R.south east);
        \draw[line width=1ex, cyan] (R.north west) -- (R.north east);
    \end{tikzpicture}%
}

\begin{document}

\noindent Some text outside Ti\emph{k}Z.

\begin{myenvii}{Some title}%
Some text This is some more to see just what happens here and what happens if there is a lit of text to see.
\end{myenvii}

\noindent$\uparrow$ note the gap disappears.

\end{document}

在此处输入图片描述

相关内容