
在我的 LaTeX 文件中,我想放置隱藏數據,例如標記或標籤。背後的想法是能夠返回明確的錯誤訊息。例如,在此乳膠文件中,我想在註釋行帶有星號的位置放置一個標籤或標記:
\documentclass[12pt]{article}
\usepackage[parfill]{parskip}
\usepackage[none]{hyphenat}
\sloppy
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{array,ragged2e, calc}
\usepackage{enumitem}
\usepackage{lipsum}
\usepackage{needspace}
\definecolor{shadecolor}{RGB}{217,217,217} % Color used for highlighting
\definecolor{light-blue}{RGB}{0,175,236} % Color for the footer
% ------------------------------------------------------------------------------------
% Header and footer management.
% ------------------------------------------------------------------------------------
\usepackage{fancyhdr}
\pagestyle{fancy}
% clear any old style settings
\fancyhf{}
\fancyheadoffset{0in}
\headheight = 53pt
\renewcommand{\headrulewidth}{0pt}
% ------------------------------------------------------------------------------------
% Defining the section style
\newcommand{\mysectionstyle}[1]{\colorbox{shadecolor}{\begin{tabular}{>{}p{\rectanglelength}}{\fontsize{13}{6}\selectfont\textbf{#1}}\end{tabular}}\vspace{6pt}}
% ------------------------------------------------------------------------------------
\addtolength{\footskip}{0.6cm}
\renewcommand{\footrulewidth}{1pt}
\renewcommand{\footrule}{{\color{light-blue}%
\vskip-\footruleskip\vskip-\footrulewidth
\hrule width\headwidth height\footrulewidth\vskip\footruleskip}}
% ------------------------------------------------------------------------------------
\usepackage[left=0.75in,top=1.5in,right=0.75in,bottom=1in]{geometry} % Document margins
\usepackage{titlesec}
\newlength{\rectanglelength}
\setlength{\rectanglelength}{\textwidth}
\addtolength{\rectanglelength}{-6pt}
\newlength{\foo}
\begin{document}
\lipsum[1]
\settototalheight{\foo}{\parbox[t]{\linewidth}{\begin{minipage}[t]{\linewidth} \begin{tabular}{@{}>{\RaggedRight}p{11cm}>{\RaggedLeft}p{6.35cm}@{}}\fontsize{13}{6}\selectfont{LaTeX}&\textbf{2011 to 2012}\end{tabular}\\\begin{tabular}{@{}>{\RaggedRight}p{11cm}>{\RaggedLeft}p{6.35cm}@{}}\textbf{Lead Programmer}&\textbf{}\end{tabular}%
\vspace{3pt}\begin{itemize}[parsep=0pt, topsep=0pt, itemsep=0pt, leftmargin=2ex]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\end{itemize}%
\vspace{7pt}\textbf{End Title}\\\lipsum[1]
\vspace{10pt}\end{minipage}}
}
\needspace{\foo}{\setlength{\parskip}{4pt}
% ******* I WANT TO INSERT A TAG OR MARKER HERE.*******
\mysectionstyle{Experiences}
\begin{tabular}{@{}>{\RaggedRight}p{11cm}>{\RaggedLeft}p{6.35cm}@{}}\fontsize{13}{6}\selectfont{LaTeX}&\textbf{2011 to 2012}\end{tabular}\\\begin{tabular}{@{}>{\RaggedRight}p{11cm}>{\RaggedLeft}p{6.35cm}@{}}\textbf{Lead Programmer}&\textbf{}\end{tabular}%
\vspace{3pt}\begin{itemize}[parsep=0pt, topsep=0pt, itemsep=0pt, leftmargin=2ex]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\end{itemize}%
\vspace{7pt}\textbf{End Title}\\\lipsum[1]
\vspace{10pt}}
\end{document}
我想在 LaTeX 文件中插入一個標籤或標記,這樣當發生錯誤時,我可以檢索標記,然後返回一條用戶友好的消息。比如,X 部分有錯誤。
在 LaTeX 中可以嗎?如果是,我該怎麼做?
答案1
我將放棄上一段所描述的用例:
我想在 LaTeX 文件中插入一個標籤或標記,這樣當發生錯誤時,我可以檢索標記,然後返回一條用戶友好的消息。比如,X 部分有錯誤。
此\show
指令將停止排版並向終端機列印下一個標記的描述。同樣,\showthe
將停止並列印計數器的值。
當排版停止時,該i
命令將允許您插入標記進行處理。
將它們放在一起,您可以編寫一個巨集來顯示節號並在出現錯誤時呼叫它。
\documentclass{article}
\usepackage{lipsum}
\makeatletter
\newcommand{\showsection}{%
\showthe\c@section
}
\makeatother
\begin{document}
\section{First}
\lipsum
\section{Second}
\lipsum
\foo% undefined cs
\section{Third}
\lipsum
\end{document}
這是一個控制台會話範例:
! Undefined control sequence.
l.20 \foo
% undefined cs
? i \showsection
> 2.
\showsection ->\showthe \c@section
l.20 \foo
% undefined cs
? s
OK, entering \scrollmode...
我i \showsection
在終端輸入行上輸入並列印出來2.
。
至於如何「標記」文檔,我將使用現有的內容並掛鉤到這些巨集中。例如,注意到我使用現有的計數器作為節號,而不是建立新的計數器。如果我想列印該部分姓名我可以向巨集新增命令\section
,將節標題儲存到巨集中,\show
然後再儲存。 (我沒有這樣做,因為如果您閱讀原始程式碼,您會發現這\section
不是一個簡單的巨集。)
答案2
你可以只做一個定義,例如
\def\thissection{section X}
然後在您控制的錯誤訊息中您可以使用該命令
\PackageError{mypackage}{You made a mistake in \thissection: try harder}{}
它會選擇目前的定義。
但是您無法自訂 TeX 本身的錯誤處理程序。例如,如果使用者輸錯命令,\fooobar
TeX 將發出未定義的命令錯誤,並且您無法自訂它使用的錯誤訊息。