# How is it possible to put hidden data in LaTeX?

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

% ------------------------------------------------------------------------------------
% ------------------------------------------------------------------------------------
\usepackage{fancyhdr}
\pagestyle{fancy}

% clear any old style settings
\fancyhf{}

% ------------------------------------------------------------------------------------
% Defining the section style
% ------------------------------------------------------------------------------------
\renewcommand{\footrulewidth}{1pt}
\renewcommand{\footrule}{{\color{light-blue}%
\vskip-\footruleskip\vskip-\footrulewidth
% ------------------------------------------------------------------------------------

\usepackage[left=0.75in,top=1.5in,right=0.75in,bottom=1in]{geometry} % Document margins
\usepackage{titlesec}

\newlength{\rectanglelength}
\setlength{\rectanglelength}{\textwidth}

\newlength{\foo}

\begin{document}
\lipsum[1]

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

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

-
Please remove the unnecessary packages. –  cyanide-based food 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
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

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.)

-

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.

-
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