1

In my manuscript, in a section/subsection/subsubsection I would like to be able to generate some sort of table of content (looks like the same mechanism) in another file with a command foo such that :

\subsection{Stuff}
some text
\foo{Inside text}
some text again
\subsubsection{Another stuff}
blah blah
\foo{Another inside text}

prints locally the "Inside text", and adds to my other file the following entry :

**Stuff**
Inside text

** Another Stuff **
Another inside text

How can I achieve that ?

1

1 Answer 1

1

Wholly adapted from my answer here: Extracting the structure of a LaTex document, including comments

Your document, here mydoc.tex

\documentclass{article}
\newcommand\addxyzline[2]{\addtocontents {xyz}{\protect \xyzline {#1}{#2}}}
\makeatletter
\newcommand\writexyz{\newcommand\xyzline[2]{}\@starttoc{xyz}}
\makeatother
\newcommand\Subsection[1]{\subsection{#1}\addxyzline{Subsection}{#1}}
\newcommand\Subsubsection[1]{\subsubsection{#1}\addxyzline{Subsubsection}{#1}}
\newcommand\foo[1]{#1\addxyzline{par}{#1}}
\begin{document}
%\tableofcontents% CAN UNCOMMMENT TO SEE THAT toc WORKS FINE
\Subsection{Stuff}

some text.
\foo{Inside Text.}
Some text again. 

\Subsubsection{Another stuff}
blah blah.
\foo{Another inside text.}
blah-blah.
\writexyz
\end{document}

Compile it to produce

enter image description here

Via the \writexyz which concludes the document, it also produces a file mydoc.xyz containing:

\xyzline {Subsection}{Stuff}
\xyzline {par}{Inside Text.}
\xyzline {Subsubsection}{Another stuff}
\xyzline {par}{Another inside text.}

which is used by the following stencil.

Second stencil file:

\documentclass{article}
\newcommand\xyzline[2]{\csname#1\endcsname {#2}\par}
\newcommand\Subsection[1]{\medskip**#1**}
\newcommand\Subsubsection[1]{\medskip** #1 **}
\begin{document}
\input{mydoc.xyz}% NAME OF .xyz FILE GOES HERE IN THE ARGUMENT
\end{document}

Compile it to produce

enter image description here

3
  • @Sebastiano Thank you so much. Likewise, blessed greetings to you! Feb 8, 2018 at 11:21
  • This is indeed exactly what I needed ! Thank you so much !
    – Bertrand R
    Feb 8, 2018 at 11:23
  • @BertrandR I am happy it works for you as desired. Feb 8, 2018 at 11:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .