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.

1
  • 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). Sep 30, 2014 at 15:39

2 Answers 2

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

2
  • 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? Sep 30, 2014 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. Sep 30, 2014 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .