Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In my LaTeX document, I would like to put hidden data, like a marker or tag. The idea behind it is to be able to return an explicit error message. For example in this latex file, I would like to put a tag or marker where the comment line is with stars:

\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}

I want to insert a tag or a marker into the LaTeX file so when an error occurred, I can retrieve the marker and then return a user-friendly message. Something like, there's an error in section X.

Is it possible in LaTeX? If yes, how can I do that?

share|improve this question
    
Please remove the unnecessary packages. –  I am who I say I am Apr 1 '13 at 16:03
    
What kind of errors are you expecting there to happen? –  egreg Apr 1 '13 at 16:21
    
You could hook into the \PackageError command itself to customize the error messages generated by packages you use. It depends on what errors you would like to catch... –  ralfix Apr 1 '13 at 16:51
    
The errors i am expecting are compiling errors such as an unknown command or bad data inside a bullet item like 'item \' –  Pete Apr 1 '13 at 17:04
1  
You may not have noticed but the default error message contains a reference to the line number in the source file where the error was found. This is often more useful information to the user than the section number, since that is where he/she will need to go to fix it. –  Matthew Leingang Apr 1 '13 at 17:18
add comment

2 Answers

up vote 3 down vote accepted

I'm going off the use case described in your last paragraph:

I want to insert a tag or a marker into the LaTeX file so when an error occurred, I can retrieve the marker and then return a user-friendly message. Something like, there's an error in section X.

The \show command will halt typesetting and print to the terminal a description of the next token. Similarly, \showthe will halt and print the value of a counter.

When typesetting halts, the i command will allow you to insert tokens for processing.

Putting these together, you can write a macro which shows the section number and call for it on errors.

\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}

Here is a sample console session:

! 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 typed i \showsection on the terminal input line and it printed 2. You can prettify the output as you like.

As far as how to "tag" the document I would use what's already in place and hook into those macros. For instance, noticed I used the existing counter for section numbers rather than created something new. If I wanted to print the section name I could add commands to the \section macro that would save the section title to a macro and \show that instead. (I didn't do this because if you read the source you will see that \section is not a simple macro.)

share|improve this answer
add comment

You can just make a definition such as

\def\thissection{section X}

then in error messages that you control you can use that command

\PackageError{mypackage}{You made a mistake in \thissection: try harder}{}

and it will pick up whatever is the current definition.

However you can not customise the error handler of TeX itself. If for example the user mistypes a command \fooobar TeX will issue its undefined command error and you can not customise the error message it uses.

share|improve this answer
    
So if i use PackageError i'll have to put all the packages that i use and put the message there? –  Pete Apr 1 '13 at 17:07
    
@Pete \PackageError is designed for a package author to use when an error in the use of a command defined in that package is used. If you are error checking code you can use \PackageError{pete} or just \GenericError{...} The point is it is an error message explictly invoked from tex code not an error due to mistyped command or missing brackets etc –  David Carlisle Apr 1 '13 at 17:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.