# Using structural commands in \newcommand - structure messed up?


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.

-
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
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
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
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
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

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.

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
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]{%

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