將 tikzpicture 錨定到其他 tikzpicture

將 tikzpicture 錨定到其他 tikzpicture

為什麼這個紅色框疊加層沒有正確錨定到矩形標題框?

  • 不要使用 xshift 或 yshift。
  • 我想要那個標題框靠在上邊距上,現在它們之間有空白。
  • 不要更改我的標題框描述中的「baseline=(headerbox.center)」(這樣我的tekst 就會居中。例如,使用「\node at (headerbox.north east)...」將紅色框錨定到它。

區塊引用

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{positioning,calc,backgrounds}
\usepackage[left=2cm,top=2cm,right=2cm,bottom=2cm,showframe]{geometry}
\begin{document}
\newcommand{\newtab}[2]{%
    \begin{tikzpicture}[overlay]
        \node[inner sep=2mm,text=white] (#1) {#2};
        \begin{scope}[on background layer]
        \draw[fill=red]%
        ($(#1.north east)$)%
        --($(#1.north west)$)%
        --($(#1.south west)$)%
        -- ($(#1.south east)$)%
        -- cycle;%
        \end{scope}%
        \end{tikzpicture}% 
}%
\noindent\begin{tikzpicture}[rounded corners=0mm, outer sep=0pt,baseline=(headerbox.center)]%
\path node[rectangle,minimum width=\textwidth-\fboxrule,minimum height=3cm,draw=black,fill=white,inner xsep=0mm,inner ysep=0in](headerbox)%
{Headertekst}%
;%
\node at (headerbox.north east)[anchor=south east,inner sep=0pt, outer sep=0pt, line width=0cm] {\newtab{headerbox}{Labeltekst}};
\end{tikzpicture}\par Test%
\end{document}

區塊引用在此輸入影像描述

答案1

如果這對您的實際文件很重要,請向下調整一半的線寬。我不認為你需要這裡的所有庫,但仍然如此。

\documentclass[10pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc,backgrounds}
\usepackage[left=2cm,top=2cm,right=2cm,bottom=2cm,showframe]{geometry}
\begin{document}
  \newcommand{\newtab}[2]{%
    \begin{tikzpicture}[overlay]
      \node[inner sep=2mm,text=white] (#1) {#2};
      \begin{scope}[on background layer]
        \draw[fill=red]%
        ($(#1.north east)$)%
        --($(#1.north west)$)%
        --($(#1.south west)$)%
        -- ($(#1.south east)$)%
        -- cycle;%
      \end{scope}%
    \end{tikzpicture}%
  }%
  \noindent\begin{tikzpicture}[rounded corners=0mm, outer sep=0pt]%
    \path node[rectangle,minimum width=\textwidth-\fboxrule,minimum height=3cm,draw=black,fill=white,inner xsep=0mm,inner ysep=0in, anchor=north](headerbox) at (current page.north)
    {Headertekst}%
    ;%
    \node at (headerbox.north east)[anchor=south east,inner sep=0pt, outer sep=0pt, line width=0cm] {\newtab{headerbox}{Labeltekst}};
  \end{tikzpicture}\par Test%
\end{document}

標籤

我認為,線條的粗細部分是人為因素,部分是因為線條寬度的一半位於圍繞文字區域繪製框架的線條上方。

答案2

我找到了一個解決方案:

  • “更乾淨”的程式碼
  • \node[overlay],覆蓋在該節點上很重要,否則該節點會佔用空間並且標題框將向下移動。

(headerbox)如果節點中沒有它,at (headerbox.north east)(headerbox) {Tekst};紅色框就不會顯示。不知道為什麼原因(headerbox.north east)明確指定了我想把紅色框放在哪裡。無論如何,這段程式碼運作良好:

區塊引用

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usepackage{calc}
\usetikzlibrary{calc,backgrounds}
\usepackage[showframe]{geometry}
\begin{document}
\noindent\begin{tikzpicture}
\node at (0,0) [rectangle,minimum width=\textwidth-\fboxrule,minimum height=5cm,draw=black,fill=white,inner xsep=0mm,inner ysep=0in,rounded corners=0mm, outer sep=0pt,baseline=(headerbox.center)](headerbox){};%
\node[overlay,inner sep=2mm,text=white,anchor=south east] at (headerbox.north east)(headerbox){Tekst};%
\begin{scope}[on background layer]%
\draw[fill=red,overlay]%
($ (headerbox.north east) $)
--($ (headerbox.north west) $)
--($ (headerbox.south west) $)
--($ (headerbox.south east) $)
-- cycle;%
\end{scope};%
\end{tikzpicture}%
\end{document}

區塊引用

相關內容