# Problem with putting an hrule in an subsubsection-like environment, when inside item environment?

\newcommand*{\example}[1]{%
\needspace{3\baselineskip}
\subsubsection{{\bf Example #1}{{\color{lightgray}\hrule width 22mm height 1.5pt}\hfill}}%
}


and it's working fine, unless if I m trying to put an \example inside an itemize or enumerate environment, in which case the line is not indented, and so it's not aligned nicely underneath the indented "Example". I thought about using a rule instead of an hrule {\color{lightgray}\rule{\linewidth}{1.5pt}}

but this doesn't seem to work inside the subsubsection. It produces an error. Any suggestions? I'd prefer to avoid using extra packages if possible. Thanks!

-

You can't use \subsubsection in an itemized or enumerated list.

Perhaps this code does what you want:

\newcounter{example}[subsection]
\renewcommand{\theexample}{\thesubsection.\arabic{example}}

\newcommand*{\example}[1]{%
\needspace{3\baselineskip}%
\refstepcounter{example}%
\noindent\textbf{\examplerule\theexample\ Example #1}\par\vspace{1ex}
\noindent\ignorespaces}
\newcommand{\examplerule}{%
\makebox[0pt][l]{\color{lightgray}\smash{\rule[-1ex]{22mm}{1.5pt}}}}


EDIT: this will draw a line as wide as the example label and also allow for \example* that doesn't print a number, nor step the example counter.

\newcounter{example}[subsection]
\renewcommand{\theexample}{\thesubsection.\arabic{example}}

\makeatletter
\newcommand{\examplename}{Example}
\newcommand*{\example}{\@ifstar{\@tempswafalse\@example}{\@tempswatrue\@example}}
\newcommand{\@example}[1]{%
\needspace{3\baselineskip}%
\if@tempswa
\refstepcounter{example}%
\def\exampletext{\textbf{\theexample\ \examplename\ #1}}%
\else
\def\exampletext{\textbf{\examplename\ #1}}%
\fi
\settowidth{\dimen0}{\exampletext}%
\noindent\examplerule\exampletext\par\vspace{1ex}}
\newcommand{\examplerule}{%
\makebox[0pt][l]{\color{lightgray}\smash{\rule[-1ex]{\dimen0}{1.5pt}}}}
\makeatletter


You can define \examplename as you wish.

-
this looks great! thanks! any easy way to change "22mm" that I had put to match the word's size? I will be changing the word example to the equivalent word in greek, and sometimes there won't be a number next to it. BTW, why do you say subsubsection is not working inside at itemized environment? the code i had included works just fine with itemize, it's the rule i was having trouble with.. – sfranky Oct 9 '11 at 13:35
I've edited the answer: you can use \example* for unnumbered examples and the rule width covers all of the label. – egreg Oct 9 '11 at 13:53
thanks very much! you've been most helpful!! – sfranky Oct 9 '11 at 17:24