<TeXmacs|1.0.7.16>

<style|article>

<\body>
  First set the environment variable "in-toc?" to 0:
  <math|\<longrightarrow\>> <assign|in-toc?|0>\ 

  Here is the macro: <math|\<longrightarrow\>>
  <assign|toc-aware|<macro|body|toc-body|<quasiquote|<if|<equal|<value|in-toc?>|1>|<unquote|<arg|toc-body>>|<unquote|<arg|body>>>>>>
  <math|\<longleftarrow\>>

  It won't work if you place it in your style file (great...)

  \;

  Examples follow:

  <section|<toc-aware|One two three|One two>>

  <subsection|<toc-aware|The formatted title|The title>>

  <subsection|<toc-aware|The third long title|The third title>>

  \;

  Now set the variable to 1: <math|\<longrightarrow\>><assign|in-toc?|1>
  <math|\<longleftarrow\>>

  And insert the TOC:

  <\table-of-contents|toc>
    <vspace*|1fn><with|font-series|bold|math-font-series|bold|1<space|2spc><if|<equal|<value|in-toc?>|1>|One
    two|One two three>> <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
    <no-break><pageref|auto-1><vspace|0.5fn>

    <with|par-left|1.5fn|1.1<space|2spc><if|<equal|<value|in-toc?>|1>|The
    title|The formatted title> <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
    <no-break><pageref|auto-2>>

    <with|par-left|1.5fn|1.2<space|2spc><if|<equal|<value|in-toc?>|1>|The
    third title|The third long title> <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
    <no-break><pageref|auto-3>>
  </table-of-contents>

  \;
</body>

<\initial>
  <\collection>
    <associate|preamble|false>
  </collection>
</initial>

<\references>
  <\collection>
    <associate|auto-1|<tuple|1|?>>
    <associate|auto-2|<tuple|1.1|?>>
    <associate|auto-3|<tuple|1.2|?>>
  </collection>
</references>

<\auxiliary>
  <\collection>
    <\associate|toc>
      <vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|1<space|2spc><if|<equal|<value|in-toc?>|1>|One
      two|One two three>> <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
      <no-break><pageref|auto-1><vspace|0.5fn>

      <with|par-left|<quote|1.5fn>|1.1<space|2spc><if|<equal|<value|in-toc?>|1>|The
      title|The formatted title> <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
      <no-break><pageref|auto-2>>

      <with|par-left|<quote|1.5fn>|1.2<space|2spc><if|<equal|<value|in-toc?>|1>|The
      third title|The third long title> <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
      <no-break><pageref|auto-3>>
    </associate>
  </collection>
</auxiliary>