add tag
Anonymous 2391
I am trying to understand how i can use custom auxiliary files to keep track of the use of new commands.
The idea is that I want to have a command that places a *question to the reader* in the text but also creates an entry in an auxiliary file, which can then be used to generate a table of *questions to the reader*. 

My first attempt creates a new counter `questionsTR` which is then used in the  [\addcontentsline](https://latexref.xyz/_005caddcontentsline.html) to write something to a file with ending `.qtr` .
```
\documentclass[a4paper,10pt]{article}

% create new counter questions to reader
\newcounter{questionsTR}

% deine a new command that creates 
\newcommand{\questiontoreader}[2]{
  \stepcounter{questionsTR}
  \textbf{Question \thequestionsTR: #1}\\ 
  #2
\addcontentsline{qtr}{questionTR}{#1}
}

\begin{document}

\questiontoreader{Very important}{This is a very important question to the reader}
\end{document}
```

However, while the `.aux` file, shown blow, has some line that indicates to me that something will be written to a `.qtr` file, no such file is ever created.

```
\relax 
\@writefile{qtr}{\contentsline {questionTR}{Very important}{1}\protected@file@percent }
```

**My Questions:**  
* Do I first have to specifically create the `.qtr` file?
* If yes, how do I create a custom auxiliary file?
* How Do I read the `.qtr` file once it is created?
* Is there a guide for something like this.
Top Answer
frougon
If you look at the code of `\@writefile`, you'll see that `\@writefile{qtr}{...}` only does `\relax` (i.e., essentially nothing) if `\tf@qtr` is undefined:

```
\long\def\@writefile#1#2{%
  \@ifundefined{tf@#1}\relax
    {%
      \add@percent@to@temptokena
        \@empty#2\protected@file@percent
        \add@percent@to@temptokena
     \immediate\write\csname tf@#1\endcsname{\the\@temptokena}%
    }%
}
```

That is why you don't see any `.qtr` file. What defines `\tf@toc` using `\newwrite`, and truncates the `.toc` file using `\immediate\openout...`, is the `\@starttoc` macro, which is called from `\tableofcontents` in `article.cls` (ditto for `\tf@lof` from `\listoffigures` and `\tf@lot` from `\listoftables`). Example from `article.cls`:

```
\newcommand\tableofcontents{%
    \section*{\contentsname
        \@mkboth{%
           \MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
    \@starttoc{toc}%
    }
```

So, you simply need to call `\@starttoc{qtr}`. For instance, with basic formatting:

```
\documentclass{article}

\makeatletter
\newcounter{questionsTR}

\newcommand{\questiontoreader}[2]{%
  \par\addvspace{1ex plus 0.2ex minus 0.2ex}%
  \noindent \refstepcounter{questionsTR}%
  \addcontentsline{qtr}{questionTR}{#1}%
  % Maybe you'll want to open a group here, use \normalfont, etc.
  \textbf{Question~\thequestionsTR: #1}\\*
  #2\par
}

% Sample formatting of list of questions entries
\newcommand{\l@questionTR}{\@dottedtocline{-10}{0em}{2.3em}}

\newcommand{\listofquestionsname}{List of questions}

\newcommand{\listofquestions}{%
  \section*{%
    \listofquestionsname
    \@mkboth{\MakeUppercase\listofquestionsname}{\MakeUppercase\listofquestionsname}%
  }%
  \@starttoc{qtr}%
}
\makeatother

\begin{document}

\listofquestions

\bigskip
\questiontoreader
  {Very important}{This is a very important question to the reader.}
\questiontoreader{Futile}{Other question here.}

\end{document}
```

![image.png](/image?hash=74d9eba5b02dc0479e35b4d99e4c7742807af67cadf1705bd53941a391e6db8b)

This sample document creates a `.qtr` file containing the following lines:

```
\contentsline {questionTR}{Very important}{1}{}%
\contentsline {questionTR}{Futile}{1}{}%
```

For the formatting of entries, cf. [source2e.pdf](http://mirrors.ctan.org/macros/latex/base/source2e.pdf):

> ```
> \@dottedtocline{⟨level⟩}{⟨indent⟩}{⟨numwidth⟩}{⟨title⟩}{⟨page⟩}
> ```
>
> Macro to produce a table of contents line with the following parameters (...).
  
Here, we specified `0em` as ⟨indent⟩ and `2.3em` as ⟨numwidth⟩, the width of the box in which the page number is set. The ⟨level⟩ we used (`-10`) is low enough that I'm sure that for any “sane” value of `tocdepth`, the questions will appear in the list of questions.

Other approaches would be:

- as Skillmon [mentioned](https://topanswers.xyz/transcript?room=2071&id=140649#c140649), using a specialized package such as `tocbasic`, `tocloft` or `etoc` (but also “[using a package is cheating](https://topanswers.xyz/transcript?room=347&id=140648#c140648)”!! ;-));

- writing to the `.aux` file or directly to the `.qtr` file.

IMHO, the main features of `\addcontentsline` are:

- it makes it so that lines in the output file (`.toc`, `.qtr`, etc.) are percent-terminated in order to avoid spurious spaces in case this file is then read while TeX is in horizontal mode (cf. clear explanations on `\protected@file@percent` in [source2e.pdf](http://mirrors.ctan.org/macros/latex/base/source2e.pdf));

- because `\addcontentsline` relies on `\addtocontents`, calls to `\label`, `\index` or `\glossary` in the third argument of `\addcontentsline` (or the second argument of `\addtocontents`) are not written to that file (they are intended for the main text, not the table of contents).

Note that even keeping the approach from the above example using macros from the LaTeX kernel, it is possible to radically change the formatting. Proof of concept:

```
\newcommand{\listofquestions}{%
  \section*{...}%
  \begingroup
  \renewcommand*{\contentsline}[4]{\noindent New entry: ##2, ##3.\par}%
  \@starttoc{qtr}%
  \endgroup
}
```

![image.png](/image?hash=4733b36c8a7fed945fc7081d38b4f157db590795674dd425d48cef1d3ae21eb4)

To answer your third question: the `.qtr` file is read when `\@starttoc{qtr}` is processed:

```
\def\@starttoc#1{%
  \begingroup
    \makeatletter
    \@input{\jobname.#1}%
    ...
```

(and immediately after this, `\@starttoc` starts to write the new `.qtr` file unless the `\if@filesw` switch is false, which can happen if `\nofiles` was used).

The LaTeX Companion has explanations on the way the toc is typeset: `\addcontentsline`, `\contentsline`, `\@dottedtocline` and the `\l@⟨type⟩` macros that specify how to format toc entries for a given hierarchical level.

P.S.: beware of spurious spaces in your code!

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.