Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is it right that usage of structural commands like \chapter, \section, \subsection and the like inside of \newcommand screws up the document's structure? I've got a \section*{...} inside a \newcommand block and using that new command does not work properly.

In Texmaker when I use the structure view to navigate through the document, clicking on the corresponding node in the tree moves the focus to the one \section command inside the \newcommand block instead of the ones that should actually be generated by usage of the custom command in different spots of the document. Complicating, I know.

I hope you'll get the point. What do I have to do to get custom commands to produce a correct document structure?

//edit: Here's an example of my problem:

\documentclass[a4paper,10pt]{report}

\newcommand{\sectionexample}[2]{
    \section*{#1}
    This is an Example of what I think is not working...
    #2
}

\begin{document}
    \tableofcontents
    \sectionexample{Test}{Hello World!}
\end{document}

This generates correct output aside from the fact that the section headline is missing in the \tableofcontents.

share|improve this question
    
Welcome to TeX.SX! Texmaker uses the sectional commands in order to build a structure tree of the document. But this has nothing to do with the way LaTeX works; it's only a commodity that some find useful (I don't, for instance). –  egreg Nov 17 '12 at 22:04
3  
Are you saying that your output is incorrect, or that TexMaker can not properly navigate it? If it is the latter I think my comments starting at Displaying current chapter/section in your editor, and the follow on comments would explain what is going on. –  Peter Grill Nov 17 '12 at 22:08
1  
Please add a minimal working example (MWE) that illustrates your problem. It is considered a lot better to put in some code that will compile, as it makes it a lot easier for us to copy it into our text editor and work with it, and see exactly what it is you are trying to do. –  Stephan Lehmke Nov 18 '12 at 5:41
1  
I think the issue is that you are using section* which not only suppresses the section number but also does not add it to the TOC. You need to do a \phantomsection (needed for hyperref) and \addcontentsline with \section* to get the unnumbered section in the TOC. –  Peter Grill Nov 18 '12 at 9:33
1  
Related: No section numbers, but still have PDF bookmarks with hyperref; some of the answers there provide solutions for getting unnumbered sections into the table of contents, independently of hyperref and PDF bookmarks. –  cyberSingularity Nov 18 '12 at 11:54

1 Answer 1

Some classes don't distinguish between \section and \section* as far as insertion in the table of contents is concerned. For instance the AMS classes behave in this way.

The standard LaTeX classes and the KoMa-Script classes, instead, do not automatically insert starred sectional commands in the table of contents.

If you use one of the standard classes, for instance report, or a class based on them, you have to "manually" do the insertion. For report it should be

\providecommand{\phantomsection}{}
\newcommand{\sectionexample}[2]{%
    \section*{#1}
    \phantomsection
    \addcontentsline{toc}{section}{#1}%
    This is an Example of what I think is not working...
    #2
}

The \phantomsection bit is needed in case you plan to use hyperref.

With the scrreprt class you can do

\newcommand{\sectionexample}[2]{%
    \addsec{#1}
    This is an Example of what I think is not working...
    #2
}

that doesn't require tricks in case hyperref is loaded.

Note that this will probably fool Texmaker's ability of showing a correct structure tree for the document. But this isn't related to LaTeX.

share|improve this answer
    
Alright, guess it's rather a Texmaker issue that made me ask. Thanks anyway! –  Hendrik Wiese Nov 22 '12 at 22:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.