[Tex/LaTex] How to create custom section titles with a “ <number>” format in ConTeXt</h1> </header><!-- .entry-header --> <p style='font-size:1.2em'><span class="mr-2 badge badge-success">context</span><span class="mr-2 badge badge-info">context-mkiv</span><span class="mr-2 badge badge-warning">sectioning</span></p> <div class="entry-content"> <p>I am making a book, with chapters titled "Week 1", "Week 2", "Week 3", etc., and sections titled "Activity 1", "Activity 2", "Activity 3", etc., and other sections titled "Presentation 1", "Presentation 2", "Presentation 3", etc.</p> <ul> <li>The preceding numbers, e.g. "1.1 Activity 1", are not used.</li> <li>No other text, e.g. "Week 1 Topic" appears in the text.</li> <li>The numbers for sections never reset.</li> </ul> <p>Here is a sample table of contents, showing how I hope it will appear:</p> <pre><code> ______________________ | | | Contents | | | | Week 1 | | Activity 1 | | Activity 2 | | Presentation 1 | | Week 2 | | Activity 3 | | Activity 4 | | Presentation 2 | |______________________| </code></pre> <p>The document occassionally has other orginary chapter and section titles as well. These should get the same font formatting, but otherwise, not be modified, and appear without any number, e.g., using chapter titles "Forward", "Conclusion", and a section title called "Notes":</p> <pre><code> ______________________ | | | Contents | | | | Forward | | Week 1 | | Activity 1 | | Activity 2 | | Presentation 1 | | Week 2 | | Activity 3 | | Notes | | Conclusion | |______________________| </code></pre> <p>I have tried my own solution, by removing the preceding numbers using <code>\setuphead[chapter][number=no]</code>, creating counters, e.g. <code>\definenumber[weekcount]</code>, then defining custom macros which call for chapter titles to use these counters, however, my solution does not work:</p> <pre><code>\definenumber[weekcount] \definenumber[activitycount] \definenumber[presentationcount] \setnumber[weekcount]{1} \setnumber[activitycount]{1} \setnumber[presentationcount]{1} \define\week{\chapter{Week~\getnumber[weekcount]}\incrementnumber[weekcount]} \define\activity{\section{Activity~\getnumber[activitycount]}\incrementnumber[activitycount]} \define\presentation{\section{Presentation~\getnumber[presentationcount]}\incrementnumber[presentationcount]} \setuphead[chapter][number=no] \setuphead[section][number=no] \starttext \week \activity This is some text. \activity This is some text. \presentation This is some text. \week \activity This is some more text. \activity This is some more text. \presentation This is some more text. \stoptext </code></pre> <p>This results in this:</p> <pre><code> ______________________ | | | Contents | | | | Week | | Activity | | Activity 1 | | Presentation | | Week | | Activity | | Activity 1 | | Presentation | |______________________| </code></pre> <p>If you remove <code>\setuphead[chapter][number=no]</code>, results are also unusual:</p> <pre><code> ______________________ | | | Contents | | | | 1. Week 1. | | 1.1 Activity 1. | | 1.2 Activity 1.1 | | 1.2 Presentation 1. | | 2. Week 2. | | 2.1 Activity 2 | | 2.2 Activity 2.1 | | 2.3 Presentation 2. | |______________________| </code></pre> <ul> <li>Why is the numbering of the counters appear in this manner, when used inside section titles?</li> <li>How can I create custom chapter titles using a "Week " format, and similarly formatted section titles?</li> </ul> </div><!-- .entry-content --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-8728145350222960" data-ad-slot="6618874009" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <div id="comments" class="comments-area"> <div class="row"> <div class="col-12"> <div class="mt-3 border-bottom border-success"> <h4 class="text-success"><i class='fa fa-check-circle text-success mr-3'></i><span>Best Answer</span></h4> </div> <div class='bg-transparent mb-3'> <p>I agree with you that exploiting the document structure is a convenient choice for this end: it offers a high degree of configurability and comes with neat extras like list (read: table of contents) building by default. So the following will be a <em>structure based</em> solution, leaving the enumerations based approach to somebody else.</p> <hr/> <p>The trick is the <em>ownnumbers</em> key of structure headers. It allows precise control over the counter mechanism for every head. Headers that are defined with <code>ownnumbers=yes</code> take a mandatory first argument by means of which their counter value can be set explicitly. Counters created by <code>\definecounter</code> each have their own <em>way</em> key that allows fine-grained control of the reset, or disabling it as required by this question (<code>way=text</code>).</p> <p>Once the counters are in place, all that’s left is to suppress placement of the structure title element. This is done in two parts: First, all headers are defined to be accessed only implicitly. The user level macros <code>\week</code>, <code>\activity</code> and <code>\presentation</code> are wrappers that at the same time pass the headers their “ownnumbers” and an empty title string. Second, placement method <code>\activityhead</code> is defined that chooses the display string from a mapping of labels, ignoring the structure title altogether.</p> <p>(Coding style is mkiv for consistency as <code>\defineheadplacement</code> does not support named arguments.)</p> <pre><code>\unprotect %% (1) define the base commands. Notice the “ownnumber” key in %% \base_presentation and \base_activity. This lets us supply an %% independent numbering scheme. \definehead [base_week] [chapter] \definehead [base_activity] [section] \definehead [base_presentation] [section] \setuphead [part] [sectionresetset=default] \setuphead [base_week] [number=yes,alternative=activityhead, sectionsegments=chapter] \setuphead [base_activity] [number=yes,alternative=activityhead, sectionsegments=section,ownnumber=yes, before={\blank[line]\incrementcounter[activity]},] \setuphead [base_presentation] [number=yes,alternative=activityhead, sectionsegments=section,ownnumber=yes, before={\blank[line]\incrementcounter[presentation]},] %% (2) define a placement method for headings that omits the title. \defineheadplacement[activityhead][vertical]#1#2{% \labeltext{\currenthead}\hskip\numberheaddistance #1% } %% (3) heading text is static so we use labels. %% The following line is cramped due to an exception in option %% parsing. %% Cf. http://archive.contextgarden.net/message/20120611.090327.dd9b1347.en.html \setuplabeltext[base_week=Week,base_activity=Activity,base_presentation=Presentation] %% (4a) define counters for use with the “presentation” and “activity” %% heads. \definecounter [presentation] [start=0,way=text] \definecounter [activity] [start=0,way=text] %% (4b) define a reset trigger for the part structural. \definestructureresetset [default] [0] [0] %% (5) define a set of wrappers. These compensate for the fact that you %% can’t have a simple structure head without the mandatory %% argument. We start with a plain one. \define \week{\base_week{}} %% The “\presentation” and “\activity” macros need special treatment %% to make it respect the separate counter. \define \activity{\base_activity {\rawcountervalue[activity]} \empty} \define\presentation{\base_presentation{\rawcountervalue[presentation]}\empty} %% (6) side effect: the table of contents needs a placement method on %% its own in order to conform with the requirements. This step is %% optional if you don’t care for a toc. \unexpanded\def\week_list#1#2#3{% \blank[line]% {\tfa\word\sc\labeltext{\currentlist}}% \space#1\hfill#3\par } \unexpanded\def\activity_list#1#2#3{% \hskip\emwidth \labeltext{\currentlist}% \space#1\hfill#3\par } \setuplist [base_week] [alternative=command,command=\week_list] \setuplist [base_activity] [alternative=command,command=\activity_list] \setuplist [base_presentation] [alternative=command,command=\activity_list] \protect \starttext \placelist[base_week, base_activity, base_presentation]\page \part {My Life ...} \week \activity Dear diary. Today I was pompous and my sister was crazy. \activity This is some text. \activity This is some text. \week \activity This is some more text. \presentation This is some more text. \activity This is some more text. \activity This is some more text. \part{... the Exciting Story of a Great Man} \week \activity This is yet some more text. \presentation This is yet some more text. \activity This is yet some more text. \stoptext </code></pre> <p><img src="https://i.imgur.com/zDi9G.png" alt="example"/></p> </div> </div> <div class="col-4"></div> </div> </div><!-- #comments --> <div id="related-embeded" class="related-embeded-area"><div class="row"><div class="col-12"><div class="mt-3 border-bottom border-success"><h4 class="text-info"><i class='fa fa-check-circle text-info mr-3'></i><span>Related Solutions</span></h4></div><div class="mt-3 mb-3 border-bottom"><h5><a href='https://imathworks.com/tex/tex-latex-continuous-v-per-chapter-section-numbering-of-figures-tables-and-other-document-elements/'>[Tex/LaTex] Continuous v. per-chapter/section numbering of figures, tables, and other document elements</a></h5></div><div class='bg-transparent mb-3'> <p>Changing the numbering of (e.g.) figures involves two modifications:</p> <ol> <li><p>Redefining whether or not the <code>figure</code> counter will be reset whenever the chapter/section counter is incremented;</p> </li> <li><p>Redefining the "appearance" of the <code>figure</code> counter (<code>\thefigure</code>), i.e., removing (or adding) the chapter/section prefix.</p> </li> </ol> <h2>Standard solution: <code>\counterwithout</code></h2> <p>The standard solution – which deals with modifications 1 and 2 mentioned above – is to use the <code>\counterwithout</code> and <code>\counterwithin</code> commands.</p> <p><sub>Since October 2018 the commands are in the LaTeX kernel; for earlier releases one needs <a href="http://ctan.org/pkg/chngcntr" rel="nofollow noreferrer"><code>\usepackage{chngcntr}</code></a></sub></p> <p>The following example shows how to achieve continuous figure numbering in the <code>book</code> class:</p> <pre><code>\documentclass{book} \counterwithout{figure}{chapter} \begin{document} \chapter{foo} \begin{figure} \centering \rule{1cm}{1cm}% placeholder for graphic \caption{A figure} \end{figure} \end{document} </code></pre> <p>Conversely, here's how to achieve per-section figure numbering in the <code>article</code> class:</p> <pre><code>\documentclass{article} \counterwithin{figure}{section} \begin{document} \section{foo} \begin{figure} \centering \rule{1cm}{1cm}% placeholder for graphic \caption{A figure} \end{figure} \end{document} </code></pre> <p>It works the same way for (e.g.) tables, custom-defined floats, equations, and footnotes. (Note that in many document classes featuring the <code>\chapter</code> command, footnotes are numbered per chapter even though the <code>footnote</code> counter does not show the chapter prefix.) The commands may also be used for theorem environments; it is easier, though, to specify the numbering of a new theorem environment when defining it:</p> <pre><code>\newtheorem{thm}{Theorem}% Continuous numbering \newtheorem{prop}{Proposition}[section]% Per-section numbering </code></pre> <p>You may also customize the numbering of the sectioning headings themselves. To, say, accomplish continuous numbering of sections in the <code>book</code> class (by default, those are numbered per chapter), but per-part numbering of chapters (which are by default numbered continuously), your preamble should contain</p> <pre><code>\counterwithout{section}{chapter} \counterwithin{chapter}{part} </code></pre> <p>To influence the resetting of counters <em>without</em> changing their appearance, use the starred macro versions <code>\counterwithout*</code> and <code>\counterwithin*</code>. E.g., for per-section numbering of figures in the <code>article</code> class – but without attaching a section prefix to <code>\thefigure</code> –, add the following to your preamble:</p> <pre><code>\counterwithin*{figure}{section} </code></pre> <p>It is also possible to redefine a counter's resetting and appearance any number of times in the document body. Note that <code>\counterwithout</code>, <code>\counterwithin</code> and their variants won't affect the counter's current value; to change the latter, use <code>\setcounter{<counter>}{<new value>}</code>.</p> <h2>AMSmath solution</h2> <p>The <a href="http://ctan.org/pkg/amscls" rel="nofollow noreferrer">AMS classes</a> and the <a href="http://ctan.org/pkg/amsmath" rel="nofollow noreferrer"><code>amsmath</code></a> package feature the <code>\numberwithin</code> macro which matches <code>\counterwithin</code>. However, there is no AMS equivalent to <code>\counterwithout</code>. Usage example: <code>\numberwithin{equation}{section}</code>. See the <a href="https://tex.stackexchange.com/a/54242/213">full example by cmhughes</a>. If you use math, you may prefer loading <code>amsmath</code> anyway and using <code>\numberwithin</code>.</p> <h2>Other solutions</h2> <p>The <a href="http://ctan.org/pkg/caption" rel="nofollow noreferrer"><code>caption</code></a> package features the key–value options <code>figurewithin</code> and <code>tablewithin</code> which allow to change the numbering of (surprise) figures and tables. Permitted option values are <code>chapter</code>, <code>section</code>, and <code>none</code>. (For the first code example above, this translates into <code>\usepackage[figurewithin=none]{caption}</code>.)</p> <p>The <a href="http://ctan.org/pkg/listings" rel="nofollow noreferrer"><code>listings</code></a> package uses <code>\AtBeginDocument</code> to define the <code>lstlisting</code> counter of the environment of the same name. To turn off the environment's per-chapter numbering for classes that feature <code>\chapter</code>, issue <code>\lstset{numberbychapter=false}</code> in the document preamble. To enable per-section numbering for classes without <code>\chapter</code>, add the following to your preamble:</p> <pre><code>\AtBeginDocument{\counterwithin{lstlisting}{section}} </code></pre> </div><div class="mt-3 mb-3 border-bottom"><h5><a href='https://imathworks.com/tex/tex-latex-remove-chapter-numbers-keeps-section-numbers/'>[Tex/LaTex] Remove Chapter numbers, keeps Section numbers</a></h5></div><div class='bg-transparent mb-3'> <p>Here is a way to do it with <code>titlesec</code> (albeit there seems to be some problem with the latest version: I wouldn't have done it exactly this way with the previous version of titlesec):</p> <pre><code>\documentclass{book} \usepackage[utf8]{inputenc} \usepackage{titlesec} \titleformat{\chapter}[block]{\bfseries\Huge}{}{0em}{} \titleformat{\section}[hang]{\bfseries\Large}{}{1em}{\thesection\enspace} \begin{document} \tableofcontents \listoffigures \chapter{First chapter} \section{First section} \begin{figure} \caption{Dummy figure} \end{figure} \end{document} </code></pre> <p><a href="https://i.stack.imgur.com/WC8tJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WC8tJ.png" alt="enter image description here"/></a></p> </div></div></div></div> </div> <div class="col-12 col-md-4"> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-8728145350222960" data-ad-slot="1340983829" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <div class='mt-3 ml-4 border-bottom border-success'><h6><span>Related Question</span></h6></div><ul class='list-group list-group-flush'></ul> </div> </div> <footer class="entry-footer"> </footer><!-- .entry-footer --> </article><!-- #post-393404 --> </div><!-- #main-col --> </div><!-- #main-row --> </div><!-- #main-container --> </main><!-- #main --> </div><!-- #primary --> </div><!-- #content --> <footer id="colophon" class="site-footer"> <div class="site-info"> </div><!-- .site-info --> </footer><!-- #colophon --> </div><!-- #page --> <script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/esm/popper.min.js?ver=1.14.7' id='popper-js'></script> <script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.min.js?ver=4.3.1' id='bootstrap4-js'></script> <script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_HTML-full&ver=2.7.5' id='mathJax-script-js'></script> <script type='text/javascript' src='https://imathworks.com/wp-includes/js/comment-reply.min.js?ver=5.9.9' id='comment-reply-js'></script> </body> </html> <!-- Page supported by LiteSpeed Cache 5.0.1 on 2024-05-06 19:55:56 -->