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.

I have made this command:

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

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
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

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.