
我想創建以下 tikzpicture:
我現在所擁有的都是這樣的:
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{tikz}
\usetikzlibrary{positioning,backgrounds}
\begin{document}
\noindent\begin{tikzpicture}[background rectangle/.style={fill=gray!25}, show background rectangle,every node/.style={inner sep=0pt,outer sep=0pt,draw}]
\begin{scope}[local bounding box=scope1]
\path[use as bounding box] (0,0) rectangle (.5\textwidth,0);
\filldraw (0,0) circle (2pt);
\node [text width=0.5\textwidth, align=left,anchor=north west](n1) {scope 1, node 1};
\node [below = 0pt of n1.south west,anchor=north west] (n2) {scope 1, node 2};
\node [below left = 0pt and 0pt of n2.north east,anchor=north west,align=left,text width=5.1cm] (n3) {scope 1, node 3 that includes longer text to be broken into lines};
\end{scope}
\begin{scope}[shift={(scope1.north east)},anchor=north west]
\node [text width=0.5\textwidth, align=left](n1) {scope 2, node 1};
\node [below = 0pt of n1.south west,anchor=north west] (n2) {scope 2, node 2};
\node [below left = 0pt and 0pt of n2.north east,anchor=north west,align=left,text width=5.1cm] (n3) {scope 2, node 3 which also includes longer text to be broken into lines};
\end{scope}
\end{tikzpicture}
\end{document}
我需要兩件事的幫助:
如何定義節點的寬度,以便和
n3
的總寬度是(即)的寬度?目前我將其設置為 5.1 厘米,因為它看起來不錯,但如果我更改節點 2 的內容,那麼節點 3 的尺寸也應該更改。如何利用為第一個範圍所建立的邊界框?n2
n3
n1
.5\textwidth
如何在兩個範圍之間插入分隔符號(例如第一張圖中用黃色繪製的分隔符號)?為什麼兩個瞄準鏡的寬度超出了
textwidth
?雖然左右有灰邊outer sep=0pt
。
答案1
背景矩形有一個填充(稱為),因此它顯示的灰色比實際內容更多。使用inner frame [xy]sep
tight background
改變這一點。
此外,線寬(任何路徑的線寬,不僅是節點的線寬,無論任何外部的sep)都會影響圖片的最終邊界框,這也會稍微影響圖片的最終位置(並會導致過度的水平盒) 。
在原始設定中,您可以使用 覆蓋邊界框的水平測量值trim left=0pt, trim right=\textwidth
。在下面的程式碼中,我使用它是±.5\textwidth
因為我創建了以原點為中心的圖片。
為了創建具有與其他節點相關的大小的節點,它們是fit
透過設定文本寬度/高度/深度來工作的庫,如果您希望節點實際具有文本,那麼這是不好的。
我的ext.positioning-plus
庫使用該庫執行類似的操作fit
,但透過使用來控制新節點的大小minimum width
,minimum height
這對於在節點中包含文字更好,但對於比節點適合的文本長的文本需要一些工作。
我在這裡使用一個非常基本的text width between
鍵,它使用calc
庫來測量物件中兩個任意點之間的距離X維度並使用它來設定text width
節點的 。我們需要inner xsep
在這裡減去兩次(這樣最終的節點實際上覆蓋了該區域和內部 xsep)。這基本上是一種設定方法固定寬度而不是最小寬度。 (對於其他形狀,係數 2 有所不同。)
為了同步節點的大小,我可以提供ext.node-families
圖書館我的tikz-ext
包裹。
每個已設定的節點node family={height=row1}
共享相同的minimum height
.這將使每個節點中的文字垂直居中對齊。
藍色節點和黃色分隔符號(也是一個節點)將跨越相同的垂直空間。
程式碼(節點系列)
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{tikz, amsmath}
\makeatletter % https://tex.stackexchange.com/a/656319/16595
\tikzset{parse let/.code={\def\tikz@cc@stop@let in{}\tikz@let@command et #1in}}
\makeatother
\usetikzlibrary{calc, positioning, backgrounds, ext.node-families}
\tikzset{
text width between/.style args={#1 and #2}{
parse let={\p@=($(#2)-(#1)$)},
text width/.expanded={abs(\x@)-2*(\noexpand\pgfkeysvalueof{/pgf/inner xsep}}}}
\begin{document}
\noindent
\begin{tikzpicture}[
background rectangle/.style={fill=gray!25},
show background rectangle, tight background,
trim left=-.5\textwidth, trim right=.5\textwidth,
every node/.style={outer sep=+0pt},
Yellow/.style={fill=yellow, node family={height=row1}},
Blue/.style ={fill=blue, node family={height=row1}, text=white},
Red/.style ={fill=red, align=center},
Green/.style ={fill=green, align=center},
node distance=+0pt
]
\node[Yellow] (sep) {};
\node[Blue, left=of sep, text width between={-.5\textwidth,0 and sep.west}]
(Blue-left) {scope 1, node 1 $\displaystyle e^x = \cfrac{1}{1 - \cfrac{x}{1 + x -
\cfrac{\frac{1}{2}x}{1 + \frac{1}{2}x - \ddots}}}$};
\node[Blue, right=of sep, text width between={ .5\textwidth,0 and sep.east}]
(Blue-right) {scope 1, node 1};
\node[Red, below right=of Blue-left.south west] (Red-left) {scope 1,\\ node 2};
\node[Red, below right=of Blue-right.south west] (Red-right) {scope 1,\\ node 2};
\node[Green, below left=of Blue-left.south east,
text width between=Red-left.east and Blue-left.east]
(Red-left) {scope 1, node 3 that includes longer text to be broken into lines};
\node[Green, below left=of Blue-right.south east,
text width between=Red-right.east and Blue-right.east]
(Red-left) {scope 2, node 3 which also includes longer text to be broken into lines
\dots\ and here's an extra linre};
\end{tikzpicture}
\end{document}
程式碼(不含節點系列)
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{tikz, amsmath}
\makeatletter % https://tex.stackexchange.com/a/656319/16595
\tikzset{parse let/.code={\def\tikz@cc@stop@let in{}\tikz@let@command et #1in}}
\makeatother
\usetikzlibrary{calc, positioning, backgrounds}
\tikzset{
text width between/.style args={#1 and #2}{
parse let={\p@=($(#2)-(#1)$)},
text width/.expanded={abs(\x@)-2*(\noexpand\pgfkeysvalueof{/pgf/inner xsep}}},
minimum height of three nodes/.style n args={3}{
parse let={\p1=($(#1.north)-(#1.south)$),
\p2=($(#2.north)-(#2.south)$),
\p3=($(#3.north)-(#3.south)$),
\n@={max(\y1,\y2,\y3)}},
minimum height/.expanded={\n@}}}
\DeclareDocumentCommand{\tikzthreenodessameheight}{O{} m O{} m O{} m}{
\node[#1,alias=@1,overlay,path only,outer ysep=+0pt]{\phantom{#2}};
\node[#3,alias=@2,overlay,path only,outer ysep=+0pt]{\phantom{#4}};
\node[#5,alias=@3,overlay,path only,outer ysep=+0pt]{\phantom{#6}};
\node[#1,minimum height of three nodes={@1}{@2}{@3}] {#2};
\node[#3,minimum height of three nodes={@1}{@2}{@3}] {#4};
\node[#5,minimum height of three nodes={@1}{@2}{@3}] {#6};}
\begin{document}
\noindent
\begin{tikzpicture}[
background rectangle/.style={fill=gray!25},
show background rectangle, tight background,
trim left=-.5\textwidth, trim right=.5\textwidth,
every node/.style={outer sep=+0pt},
Yellow/.style={fill=yellow},
Blue/.style ={fill=blue, text=white},
Red/.style ={fill=red, align=center},
Green/.style ={fill=green, align=center},
node distance=+0pt
]
\tikzthreenodessameheight
[Yellow, name=sep]{}
[Blue, left=of sep, name=Blue-left,
text width between={-.5\textwidth,0 and sep.west}]
{scope 1, node 1 $\displaystyle e^x = \cfrac{1}{1 - \cfrac{x}{1 + x -
\cfrac{\frac{1}{2}x}{1 + \frac{1}{2}x - \ddots}}}$}
[Blue, right=of sep, text width between={ .5\textwidth,0 and sep.east}, name=Blue-right]
{scope 1, node 1};
\node[Red, below right=of Blue-left.south west] (Red-left) {scope 1,\\ node 2};
\node[Red, below right=of Blue-right.south west] (Red-right) {scope 1,\\ node 2};
\node[Green, below left=of Blue-left.south east,
text width between=Red-left.east and Blue-left.east]
(Red-left) {scope 1, node 3 that includes longer text to be broken into lines};
\node[Green, below left=of Blue-right.south east,
text width between=Red-right.east and Blue-right.east]
(Red-left) {scope 2, node 3 which also includes longer text to be broken into lines
\dots\ and here's an extra linre};
\end{tikzpicture}
\end{document}
輸出
答案2
預設情況下,使用tcbraster
(或tcbitemize) from
tcolorbox , we can leave the package to do all the work for us. The
raster use
\texwidth` ,每個柵格列都具有相等的寬度,但也可以手動調整它們。
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage[most]{tcolorbox}
\begin{document}
\begin{tcbitemize}[raster every box/.style={sharp corners,
fontupper=\sffamily, colupper=white, boxrule=0pt, halign=center},
raster left skip=0pt, raster right skip=0pt,
raster before skip=0pt, raster row skip=0pt,
raster after skip=0pt, raster valign=top]
\tcbitem[colback=blue!90!black] scope 1, node 1
\tcbitem[colback=blue!90!black] scope 2, node 1
\tcbitem[blankest, raster column skip=0pt]
\begin{tcbitemize}[raster force size=false]
\tcbitem[colback=red!90!black, width=.4\linewidth] scope 1, node 2
\tcbitem[colback=green!90!black, width=.6\linewidth] scope 1, node 3 that includes longer text to be broken into lines
\end{tcbitemize}
\tcbitem[blankest, raster column skip=0pt]
\begin{tcbitemize}[raster force size=false]
\tcbitem[colback=red!90!black, width=.4\linewidth] scope 2, node 2
\tcbitem[colback=green!90!black, width=.6\linewidth] scope 2, node 3 which also includes longer text to be broken into lines
\end{tcbitemize}
\end{tcbitemize}
\end{document}
答案3
我試圖接近您的初始代碼。但有一個問題沒有考慮:如果右上節點的高度大於左上節點的高度會發生什麼事。解決方案可能是反轉構造並以右上節點作為參考。
程式碼
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{calc, positioning}
\begin{document}
\lipsum[1]
\bigskip
\tikzset{%
T/.style={blue!50!black, fill=blue!50!black, text=white,
minimum width=0.48\textwidth, text width=0.48\textwidth-1ex
},
BW/.style={green!50!black, fill=green!70!black, text=black,
minimum width=0.17\textwidth, text width=0.17\textwidth-1ex
},
BE/.style={red!75!black, fill=red!95!black, text=black,
minimum width=0.30\textwidth, text width=0.30\textwidth-1ex,
}
}
\noindent\begin{tikzpicture}[%
every node/.style={draw, inner sep=1ex, outer sep=0pt, align=center,
minimum height=7ex}
]
\node[T, anchor=north west] at (0, 0)
(nTW) {\bfseries \lipsum[2]};
\node[BW, below=0pt of nTW.south west, anchor=north west]
(n2) {scope 1, node 2};
\node[BE, below left=0pt and 0pt of n2.north east, anchor=north west]
(n3) {scope 1, node 3 that includes longer text to be broken into lines};
\path ($(nTW.south) -(nTW.north)$);
\pgfgetlastxy{\newW}{\newH}
\node[T, right=.02\textwidth of nTW.north east,
anchor=north west, minimum height={-\newH}]
(nTE) {\bfseries scope 2, node 1};
\node[BW, below=0pt of nTE.south west, anchor=north west]
(n2E) {scope 2, node 2};
\node[BE, below left=0pt and 0pt of n2E.north east, anchor=north west]
(n3E) {scope 2, node 3 that includes longer text to be broken
into lines; can be longer than the West corresponding node};
\fill[yellow!90!red] (nTW.north east) rectangle (nTE.south west);
\end{tikzpicture}
\end{document}
答案4
替代建議。我的程式碼技術性較差(但也許也不太優雅?)。
您可以調整黃色分隔符的寬度(\yellowGap
在我的程式碼中),以及節點 1 的寬度百分比(percentAmount
0 到 100 之間的整數,但最好不要選擇太接近 0 或 100 的值,以避免非常窄列) ,取得節點2的寬度。
最後,您可以調整節點中內部 sep 的長度\innerXSep
。
\documentclass[a4paper,10pt]{article}
\usepackage[showframe]{geometry}
\usepackage[x11names]{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{calc}
\newlength{\yellowGap}
\setlength{\yellowGap}{3mm}
\newcounter{percentAmount}
\setcounter{percentAmount}{40}% =40% of the width of the first node. Value needs to be integer
\newlength{\innerXSep}
\setlength{\innerXSep}{.3333em}% default value
\newlength{\mainNodeWidth}
\setlength{\mainNodeWidth}{(\textwidth-\yellowGap)/2}
\newlength{\secondNodeWidth}
\setlength{\secondNodeWidth}{\mainNodeWidth*\value{percentAmount}/100}
\newlength{\thirdNodeWidth}
\setlength{\thirdNodeWidth}{\mainNodeWidth-\secondNodeWidth}
\begin{document}
\noindent\begin{tikzpicture}[outer sep=0pt,inner xsep=\innerXSep,text=white]
\node[text width=\mainNodeWidth-2\innerXSep,fill=DodgerBlue3,align=center] (mainLeft) {Scope 1, Node 1};
\node[text width=\secondNodeWidth-2\innerXSep,fill=Firebrick2,align=center,anchor=north west] (secondLeft) at (mainLeft.south west) {Scope 1,\\Node 2};
\node[text width=\thirdNodeWidth-2\innerXSep,fill=Green3,align=center,anchor=north west] (thirdLeft) at (secondLeft.north east) {Scope 1, Node 3\\ that includes longer text to be broken into lines};
\fill[DarkGoldenrod1] (mainLeft.north east) rectangle ($(mainLeft.south east)+(\yellowGap,0)$);
\node[text width=\mainNodeWidth-2\innerXSep,fill=DodgerBlue3,align=center,anchor=north west] (mainRight) at ($(mainLeft.north east)+(\yellowGap,0)$) {Scope 2, Node 1};
\node[text width=\secondNodeWidth-2\innerXSep,fill=Firebrick2,align=center,anchor=north west] (secondRight) at (mainRight.south west) {Scope 2,\\Node 2};
\node[text width=\thirdNodeWidth-2\innerXSep,fill=Green3,align=center,anchor=north west] at (secondRight.north east) {Scope 2, Node 3\\ which also includes longer text to be broken into lines};
\end{tikzpicture}
\end{document}