
我希望在 TikZ 矩陣中設定最大列數,如果我在達到最大計數時啟動一個新單元格,LaTeX 會將單元格放入新行中。
一種方法是使用命令檢查目前列號並確定是否已替換為 an&
或 a \\
。
背景:
我正在製作 PDF 以便在一系列不干膠標籤上列印,如下所示:
建立標籤時,如果當前行已滿,我必須插入\\
而不是插入,這很不方便。&
有時我想修改我的文件,即刪除一個項目,並且我必須移動每個後續項目\\
以使換行符正確。
更新:
程式碼發佈在這裡以提供更多解釋。我想讓元素的每個位置都在我的控制之下,因此我將矩陣相對於紙張邊緣錨定。如果沒有印表機造成的偏移,則下列程式碼產生的邊界框將與紙張上的預切縫重合。
\documentclass[a4paper]{article}
\usepackage{qrcode}
\usepackage{tikz}
\usepackage{hyperref}
\usepackage{xifthen} % provides \isempty test
\usepackage{pst-barcode}
\usetikzlibrary{positioning}
\pagestyle{empty}
\newcommand{\Label}[2][]{
\draw (0cm,0cm) rectangle (3cm,-4cm) node (framesoutheast) {};
\path (0cm,0cm) -- node[midway,anchor=north,inner sep=0pt,yshift=-4mm] (qrcode) {
\begin{pspicture}(0.9in,0.9in)
\psbarcode{https://wiki.thu-skyworks.org/#2}{width=0.9 height=0.9}{qrcode}
\end{pspicture}
%\qrcode[2cm]{https://wiki.thu-skyworks.org/#2}
%\XeTeXinputencoding "utf8"
}(3cm,0cm);
\node[anchor=south west,inner sep=1mm,outer sep=0pt] (logo) at (0mm,-40mm) {\includegraphics[width=9mm]{any_square_image}};
\path (logo.east) -- node[anchor=center,text width=19mm,align=center,font=\sffamily\small]{\ifthenelse{\isempty{#1}}{#2}{#1}}
(logo.east-|framesoutheast);
}
\begin{document}
\begin{tikzpicture}[remember picture,overlay]
\matrix[row sep={4cm,between origins}, column sep = {32mm,between origins},matrix anchor=north west,inner sep=0pt,xshift=10mm,yshift=-8mm] at (current page.north west) {
%\XeTeXinputencoding "byte"
\Label{Label\ one} & \Label[If need\\manual break]{If\ need\ manual\ break} & \Label{Label\ 3} &
\Label{Label 4} & \Label{Label 5} & \Label{Label 6} \\ % I want to make this line break auto
\Label{Label 7} \\
};
\end{tikzpicture}
\end{document}
答案1
您可以使用計數器來對標籤進行計數。每個標籤之後計數器就會增加。如果計數器為 6,則插入\\
並重設計數器,否則插入(或更確切地說,是矩陣中的&
定義,即)。&
\pgfmatrixnextcell
\newcounter{colnr}
\setcounter{colnr}{0}
\newcommand{\lb}{%
\stepcounter{colnr}%
\ifthenelse{\value{colnr}=6}{\setcounter{colnr}{0}\\}{\pgfmatrixnextcell}%
}
將矩陣中的所有&
和替換為,最後一個除外。\\
\lb
\\
答案2
作為矩陣的替代方案,TiKZ
您可以使用tcbraster
的構造tcolorbox
。您定義raster columns
值並列出所有元素。tcbraster
將分發它們並在需要時斷行和分頁。
以下程式碼顯示如何聲明它。選項raster force size=false
保持內部框的大小,否則內部raster columns
框將縮放以適合一個文字行。
\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage[hmargin={1cm,1cm},vmargin={1cm,1cm}]{geometry}
\begin{document}
\begin{tcbraster}[raster columns=6, raster every box/.style={blankest, width=3cm}, raster force size=false]
\foreach \i in {1,...,20}{%
\tcbincludegraphics{example-image}
\tcbincludegraphics{example-image-A}
\tcbincludegraphics{example-image-B}
\tcbincludegraphics{example-image-C}
}
\end{tcbraster}
\end{document}