
考慮下面的 MWE。這會產生一個具有 2 個重疊行的矩陣(這就是我想要的),但是,它也在左側和右側創建了額外的空白(我在下面的螢幕截圖中將其標記為紅色)。
如何在最小程度地改變 MWE 的情況下刪除此左/右「紅色」空白?那麼如何獲得與上/下相同的左/右行為呢?
微量元素
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\setlength{\fboxsep}{0pt}
\fbox{
\centering
\begin{tikzpicture}[ampersand replacement=\&,%
grayspacer/.style={%
draw=none,%
fill=gray,%
inner sep=0pt,%
},%
matstyle/.style={%
matrix of nodes,%
nodes={%
draw=black,%
rectangle%
,minimum size=0.5cm,%
fill=black%
},%
nodes in empty cells,%
column sep=0.55cm,%
row sep=-5pt,
inner sep=0pt%
},%
>=latex%
]%
\matrix[matstyle] (mat)
{
\& |[grayspacer]| \& |[grayspacer]| \& \\
|[grayspacer] | \& \& \& |[grayspacer]| \\
};
\end{tikzpicture}
}
\end{document}
答案1
問題不在於 tikz 圖片,這沒關係,而是\fbox{}
tikz 圖片周圍有空格。看:
\fbox{
\centering
\begin{tikzpicture}% Omitted
% ... Omitted
\end{tikzpicture}
}
之後的換行符\fbox{
在 TeX 中被翻譯成一個空格。後面的換行符號也會發生同樣的情況\end{tikzpicture}
。為了避免這種情況,您必須「註解」這些換行符,如下所示:
\fbox{%
\centering
\begin{tikzpicture}% Omitted
% ... Omitted
\end{tikzpicture}%
}
%
請注意,在 tikzpicture 內部,這些技巧不是必需的,因為 tikz 解析器會忽略所有換行符,因此不需要在幾行末尾使用大多數換行符。
我認為你想要的draw=none
不是draw=black
黑色方塊,而是避免與灰色框的錯位,我認為這是無意的)。另外,由於 fbox 很緊,所以沒有理由\centering
.最終文件:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\setlength{\fboxsep}{0pt}
\fbox{%
\begin{tikzpicture}[ampersand replacement=\&, >=latex,
grayspacer/.style={draw=none, fill=gray, inner sep=0pt,},
matstyle/.style={ matrix of nodes,
nodes={draw=none, rectangle, minimum size=0.5cm, fill=black},
nodes in empty cells, column sep=0.55cm, row sep=-5pt,inner sep=0pt,
},
]
\matrix[matstyle, inner sep=0pt] (mat)
{
\& |[grayspacer]| \& |[grayspacer]| \& \\
|[grayspacer] | \& \& \& |[grayspacer]| \\
};
\end{tikzpicture}%
}
\end{document}
結果:
答案2
在這種特殊情況下,a\fbox
是剩餘的。您可以透過以下matrix
選項獲得相同的邊框:draw
matstyle
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[%ampersand replacement=\&, >=latex,
matstyle/.style = {matrix of nodes,draw,
nodes={draw=none, rectangle, minimum size=0.5cm, fill=black},
nodes in empty cells,
column sep=0.5cm, row sep=0pt,
},
]
\matrix[matstyle, inner sep=0pt] (mat)
{
& |[gray]| & |[gray]| & \\
|[gray]| & & & |[gray]| \\
};
\end{tikzpicture}
\end{document}