我是 TeX 新手。我希望排版任意数量的小标签(18 毫米 x 7 毫米),这些标签基于存储在 CSV 文件中的字母数字字符串迭代生成。每个标签都应包含一个二维码及其关联的字母数字字符串。使用我在网上找到的一些 XeLaTeX 代码,我创建了一个 for 循环,它从 CSV 文件中提取字母数字字符串,并在漂亮的小框内迭代生成二维码。
我的问题是:我可以在指定的边距内将 14 个标签放在文档的一行上。如果我在 CSV 文件中包含超过 14 个字母数字代码,XeLaTeX 只会将行延续到文档页面的末尾。我希望 XeLaTeX 在到达右侧边距时自动转到新行。如果我可以指定每行要打印的标签数量,那也可以。
这是我的代码:
\documentclass[10pt]{article}
\usepackage{fontspec} %allows for small font
\usepackage[landscape, lmargin=6mm, tmargin=5.5mm]{geometry} %small margins, landscape orientation
\usepackage{qrcode} %QR code generation
\usepackage{readarray} %reads contents of CSV file
%% Font
\setmainfont[Scale=0.5, PunctuationSpace=3, WordSpace = 0.3]{Calibri}
%Sets margins for individual labels
\renewcommand{\fboxsep}{0.2mm}
%Typesetting labels
\newcommand{\viallabel}[1]{\framebox[18mm][c]{\parbox{18mm}{\raggedright #1}}}
\setlength{\columnsep}{0.5mm}
\setlength{\parindent}{0mm}
%
\begin{document}
%Reads a CSV file, ports it into TeX as a macro, then iteratively creates labels using a for loop
\readarraysepchar{\\}
\readdef{K:/QR.codes/labels.csv}\labelsmacro
\ignoreemptyitems
\setsepchar{\\/,}
\readlist\mylist{\labelsmacro}
\foreachitem\x\in\mylist{\viallabel{\hspace{2mm}\qrcode[height=7mm]{\x}\hspace{1mm}{\x}}}
\end{document}
这是我的 CSV 文件:
MF00001
MF00002
MF00003
MF00004
MF00005
MF00006
MF00007
MF00008
MF00009
MF00010
MF00011
MF00012
MF00013
MF00014
MF00015
MF00016
MF00017
MF00018
MF00019
MF00020
这是生成的文档的图像:
谢谢!
答案1
你把很多盒子直接放在了彼此的后面。它们之间没有断点,所以 TeX 无法换行,并且会溢出。
您可以插入一个带有空格(可以为零)或惩罚的断点。
\newcommand{\viallabel}[1]{\framebox[18mm][c]{\parbox{18mm}{\raggedright #1}}\hspace{0pt}}
如果你的盒子不能完美地容纳,那么使用 \raggedright 来避免过满的行通常也是有意义的:
\documentclass{article}
\usepackage{pgffor}
\begin{document}
\foreach\x in {1,2,...,40}
{\fbox{xxx!}}
\medskip
\foreach\x in {1,2,...,40}
{\fbox{hhh!}\hspace{0pt}}
\medskip
\foreach\x in {1,2,...,40}
{\fbox{ppp!}\penalty0 }
\raggedright
\medskip
\foreach\x in {1,2,...,40}
{\fbox{hhh!}\hspace{0pt}}
\medskip
\foreach\x in {1,2,...,40}
{\fbox{ppp!}\penalty0 }
\end{document}
答案2
您可以简化设置(不用readarray
或expl3
改为):
\begin{filecontents*}{\jobname.csv}
MF00001
MF00002
MF00003
MF00004
MF00005
MF00006
MF00007
MF00008
MF00009
MF00010
MF00011
MF00012
MF00013
MF00014
MF00015
MF00016
MF00017
MF00018
MF00019
MF00020
\end{filecontents*}
\documentclass[10pt]{article}
\usepackage{fontspec} %allows for small font
\usepackage[landscape, lmargin=6mm, tmargin=5.5mm]{geometry} %small margins, landscape orientation
\usepackage{qrcode} %QR code generation
%% Font
\setmainfont[Scale=0.5, PunctuationSpace=3, WordSpace = 0.3]{Calibri}
%Sets margins for individual labels
\renewcommand{\fboxsep}{0.2mm}
\setlength{\parindent}{0mm}
%Typesetting labels
\newcommand{\printlabel}[1]{%
\framebox[18mm][l]{%
\hspace{2mm}%
\qrcode[height=7mm]{#1}%
\hspace{1mm}%
#1%
}%
}
\ExplSyntaxOn
\NewDocumentCommand{\printlabelsfromfile}{m}
{% #1 = file name
\yonathan_labels_print:n { #1 }
}
\ior_new:N \g_yonathan_labels_file_ior
\seq_new:N \l_yonathan_labels_seq
\cs_new_protected:Nn \yonathan_labels_print:n
{
\ior_open:Nn \g_yonathan_labels_file_ior { #1 }
\ior_str_map_inline:Nn \g_yonathan_labels_file_ior
{
\printlabel{##1}\hspace{0pt}
}
}
\ExplSyntaxOff
\begin{document}
\raggedright\setlength{\lineskip}{4mm}
\printlabelsfromfile{\jobname.csv}
% an example with reduced width
\bigskip
\begin{minipage}{0.3\textwidth}
\raggedright\setlength{\lineskip}{4mm}
\printlabelsfromfile{\jobname.csv}
\end{minipage}
\end{document}
如果希望行与行之间没有间隙,请将设置更改\lineskip
为。0pt
答案3
\allowbreak
只需在循环末尾添加(允许换行) \foreachitem
,方式如下\foreachitem\x\in\mylist{\viallabel{\hspace{2mm}\qrcode[height=7mm]{\x}\hspace{1mm}{\x}}\allowbreak}
\begin{filecontents*}{labels.csv}
MF00001
MF00002
MF00003
MF00004
MF00005
MF00006
MF00007
MF00008
MF00009
MF00010
MF00011
MF00012
MF00013
MF00014
MF00015
MF00016
MF00017
MF00018
MF00019
MF00020
\end{filecontents*}
\documentclass[10pt]{article}
\usepackage{fontspec} %allows for small font
\usepackage[landscape, lmargin=6mm, tmargin=5.5mm]{geometry} %small margins, landscape orientation
\usepackage{qrcode} %QR code generation
\usepackage{readarray} %reads contents of CSV file
%% Font
\setmainfont[Scale=0.5, PunctuationSpace=3, WordSpace = 0.3]{Calibri}
%Sets margins for individual labels
\renewcommand{\fboxsep}{0.2mm}
%Typesetting labels
\newcommand{\viallabel}[1]{\framebox[18mm][c]{\parbox{18mm}{\raggedright #1}}}
\setlength{\columnsep}{0.5mm}
\setlength{\parindent}{0mm}
%
\begin{document}
%Reads a CSV file, ports it into TeX as a macro, then iteratively creates labels using a for loop
\readarraysepchar{\\}
\readdef{labels.csv}\labelsmacro
\ignoreemptyitems
\setsepchar{\\/,}
\readlist\mylist{\labelsmacro}
\foreachitem\x\in\mylist{\viallabel{\hspace{2mm}\qrcode[height=7mm]{\x}\hspace{1mm}{\x}}\allowbreak}
\end{document}