我正在使用以下代码让 LaTeX 填写银行付款的纸质表格。表格中的单个单元格具有最大宽度,因此我的问题是:我可以让 LaTeX 突出显示超出预定义宽度的文本序列(例如用红色)吗?
例如,如果“帐户持有人”文本太长,我希望它在 PDF 中以红色字体显示。
\documentclass[12pt]{scrartcl}
\usepackage[a6paper,landscape,left=0cm,top=0cm,right=0cm,bottom=0cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{ocr}
\usepackage[absolute]{textpos}
\setlength{\TPHorizModule}{1cm}
\setlength{\TPVertModule}{1cm}
\setlength{\parindent}{0cm}
\setlength{\parskip}{0cm}
\begin{document}\ocrfamily
\begin{textblock}{3.5}[0,0](0.8,3.5)1234567890\end{textblock}
\begin{textblock}{6}[0,0](4.8,3.4)Account holder\end{textblock}
\begin{textblock}{2.5}[0,0](11.75,3.5)40,00\end{textblock}
\begin{textblock}{12}[0,0](0.75,5)Some Description\end{textblock}
\end{document}
答案1
对 Fran 的代码进行稍微进一步的分析,可以测试项目的长度并将其与分配的大小进行比较,并且只有\fbox
超出时才执行。
\documentclass[12pt]{scrartcl}
\usepackage[a6paper,landscape,left=0cm,top=0cm,right=0cm,bottom=0cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{ocr}
\usepackage{xcolor}
\usepackage[absolute]{textpos}
\setlength{\TPHorizModule}{1cm}
\setlength{\TPVertModule}{1cm}
\setlength{\parindent}{0cm}
\setlength{\parskip}{0cm}
\newcommand\BankForm[4]{%
\begin{textblock}{#1}[0,0](#2,#3)%
\makebox[0pt][l]{#4}%
\setbox0=\hbox{#4}%
\ifdim\wd0>\hsize
{\color{red}\fbox{\makebox[\hsize][l]{\phantom{#4}}}}% Uncomment to show edges
\fi
\end{textblock}
}
\begin{document}\ocrfamily
\BankForm{3.5}{0.8}{3.5}{1234567890}
\BankForm{4}{4.8}{3.5}{Too long account}
\BankForm{2.5}{11.75}{3.5}{40}
\BankForm{12}{0.75}{5}{Some Description}
\end{document}
如果需要增加强调程度,也可以将文本本身设为突出显示颜色:
\documentclass[12pt]{scrartcl}
\usepackage[a6paper,landscape,left=0cm,top=0cm,right=0cm,bottom=0cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{ocr}
\usepackage{xcolor}
\usepackage[absolute]{textpos}
\setlength{\TPHorizModule}{1cm}
\setlength{\TPVertModule}{1cm}
\setlength{\parindent}{0cm}
\setlength{\parskip}{0cm}
\newcommand\BankForm[4]{%
\begin{textblock}{#1}[0,0](#2,#3)%
\makebox[0pt][l]{#4}%
\setbox0=\hbox{#4}%
\ifdim\wd0>\hsize
\textcolor{red}{\makebox[0pt][l]{#4}%
\fbox{\makebox[\hsize][l]{\phantom{#4}}}}% Uncomment to show edges
\else
\makebox[0pt][l]{#4}%
\fi
\end{textblock}
}
\begin{document}\ocrfamily
\BankForm{3.5}{0.8}{3.5}{1234567890}
\BankForm{4}{4.8}{3.5}{Too long account}
\BankForm{2.5}{11.75}{3.5}{40}
\BankForm{12}{0.75}{5}{Some Description}
\end{document}
答案2
检查的方法是显示一个与文本块水平大小相同的框。在此 MWE 中,您只需取消注释第 16 行即可显示红色框。
\documentclass[12pt]{scrartcl}
\usepackage[a6paper,landscape,left=0cm,top=0cm,right=0cm,bottom=0cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{ocr}
\usepackage{xcolor}
\usepackage[absolute]{textpos}
\setlength{\TPHorizModule}{1cm}
\setlength{\TPVertModule}{1cm}
\setlength{\parindent}{0cm}
\setlength{\parskip}{0cm}
\newcommand\BankForm[4]{%
\begin{textblock}{#1}[0,0](#2,#3)%
\makebox[0pt][l]{#4}%
%{\color{red}\fbox{\makebox[\hsize][l]{\phantom{#4}}}}% Uncomment to show edges
\end{textblock}
}
\begin{document}\ocrfamily
\BankForm{3.5}{0.8}{3.5}{1234567890}
\BankForm{4}{4.8}{3.5}{Too long account}
\BankForm{2.5}{11.75}{3.5}{40}
\BankForm{12}{0.75}{5}{Some Description}
\end{document}