4

I'm trying to create a new command or environment or something to format what is essentially a section, but it needs to have it's own counter. I would like to not use any of the \section \subsection etc. as I go into the full depth that they provide.

The idea being I would type in something similar to...

\session{Hello World}

... and it would output something like (although centred) ...

Session 1 : Hello World

The command I've currently got is as follows:

\newcounter{sessioncounter}
\newcommand{\session}
{
\begin{center}
\begin{emph}
\begin{textbf}
\begin{Large}
Session \value{sessioncounter}\stepcounter{sessioncounter}: 
}{
\end{Large}
\end{textbf}
\end{emph}
\end{center}
}

Trying this exactly, the Latex compiler throws the following error (note, this is before \begin{document} has even been called).

LaTeX Error: \begin{document} ended by \end{Large}

I've also tried creating a new environment by literally swapping out \newcommand{\session} to \newenvironment{session}. While this compiles, I get the following error on the line of \begin{session}.

Missing \endcsname inserted.

        <to be read again> 

\aftergroup

l.13 \begin{session}

Can anyone see where I'm going wrong? I presume by the error that newcommand can't be used exactly with this syntax for what I want; however, I'm also confused as to why the environment does not work either.

  • 2
    \newcommand would define a command \session you are defining an environment so \newenvironment{session} (and unrelated but you want % at the end of all the lines in the definition). – David Carlisle Sep 30 '14 at 15:39
4

Your definition matches the format of an environment, not a command.

This is how environments are defined:

\newenvironment{example}{<starting commands>}{<ending commands>}

They are then used like this:

\begin{example}
<text>
\end{example}

But I think you want a command that takes one argument, as in this example. Also, \thesessioncounter gives you the number as text; \value is for use in other internal commands. (I've adjusted the formatting commands a bit as well.)

\documentclass{article}
\newcounter{sessioncounter}
\newcommand{\session}[1]{%
    \hfil\bgroup\Large\itshape\bfseries 
    Session~\thesessioncounter: #1\egroup\par\bigskip
    \stepcounter{sessioncounter}%
}

\begin{document}
\session{Hello World}
\session{Hello again}
\session{Hello for the last time}
\end{document}

enter image description here

  • Thanks, got few questions from your formatting though if you don't mind. What do the %s do? I gather the \bgroup and \egroup is an alternative to a 'scope' (excuse my C++ terminology). What's the ~ used for? – AdmiralJonB Sep 30 '14 at 22:07
  • % tells TeX to ignore the subsequent EOL; \bgroup and \egroup are the same as { and } and yes, limit the scope of the commands in between; ~ indicates a non-breaking space, typically used before the numeral in cases like this. – musarithmia Sep 30 '14 at 23:29
4

You can't do \begin{textbf} or \begin{emph} and you shouldn't do \begin{Large} either.

You want

\newcommand{\session}[1]{%
  \begin{center}\Large\itshape\bfseries
    \stepcounter{sessioncounter}%
    Session \value{sessioncounter}: #1%
  \end{center}%
}

in the preamble and use

\session{Hello world}

in the document.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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