GeneralExamples in LaTeX Companion

LaTeX specific issues not fitting into one of the other forums of this category.
NinV
Posts: 69
Joined: Sun Oct 05, 2008 12:57 pm

Examples in LaTeX Companion

Post by NinV »

Hi,

I often browse through The LaTeX Companion (I still need help!) and now I would like to number the examples of one of my document using the same method used by TLC's authors, but in this book I didn't succeed to get any explaination (I've browsed this forum, too).

Can someone help me?

Thanks a lot,
Nino

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Re: Examples in LaTeX Companion

Post by gmedina »

Hi,

how exactly are you building your examples? Are you using a custom environment, or some environment defined with the help of amsthm? Please post a minimal compilable example of the code that you are using to create your examples.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
NinV
Posts: 69
Joined: Sun Oct 05, 2008 12:57 pm

Examples in LaTeX Companion

Post by NinV »

Hi,

now I'm using ntheorem, but I would like to adopt the style used by TLC's authors. It seems to be a modified equation environment.
I attach a screenshot of one example taken from that book:
tlc.png
tlc.png (28.74 KiB) Viewed 5508 times
As you can see at the end (or at the beginning in an odd page) of the last line of the example there is a label with the chapter and section numbers followed by a progressive number.

Thanks,
Nino
Window + MiKTeX 2.7
Ubuntu + TexLive + Kile
Xandros (eeePC) + TeTex + TexMaker
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Examples in LaTeX Companion

Post by gmedina »

Hi,

the code below shows how to proceed schematically:

Code: Select all

\documentclass[openany]{book}
\usepackage{chngcntr}

% creating the counter
\newcounter{exmpl}
% subordinating the new counter to sections; requires the chngcntr package
\counterwithin{exmpl}{section}
% redefining the counter. Format: chapter-section-exmpl
\renewcommand\theexmpl{\arabic{chapter}--\arabic{section}--\arabic{exmpl}}
% defining the environment for examples
\newenvironment{myexample}
  {\stepcounter{exmpl}\noindent}
  {\marginpar{\fbox{\theexmpl}}\vskip1em}

\begin{document}

\chapter{Test chapter one}
\section{Test section one one}
\begin{myexample}
  Test example one
\end{myexample}
\begin{myexample}
  Test example two
\end{myexample}
\section{Test section one two}
\begin{myexample}
  Test example three
\end{myexample}
\chapter{Test chapter two}
\section{Test section two one}
\begin{myexample}
  Test example four
\end{myexample}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
NinV
Posts: 69
Joined: Sun Oct 05, 2008 12:57 pm

Re: Examples in LaTeX Companion

Post by NinV »

Thank you very much! It works fine.

I'm a little frustrated because I'm not able to solve this type of problems :(

Thank you again,
Nino
Window + MiKTeX 2.7
Ubuntu + TexLive + Kile
Xandros (eeePC) + TeTex + TexMaker
NinV
Posts: 69
Joined: Sun Oct 05, 2008 12:57 pm

Examples in LaTeX Companion

Post by NinV »

Hi gmedina,
your code works very well, but I've tried to modify ntheorem command too :)

Code: Select all

\makeatletter
\renewtheoremstyle{marginbreak}%
{\item[\rlap{\vbox{\hbox{                      \theorem@headerfont                      \llap{\fbox{##1 ##2}}                     \hskip\labelsep\relax                    \theorem@separator}            \hbox{\strut}}}]}  {\item[\rlap{\vbox{\hbox{\theorem@headerfont
    \llap{\fbox{##1 ##2}}\hskip\labelsep\relax\
    ##3\theorem@separator}\hbox{\strut}}}]}
\makeatother
and I've got the following:
tlc2.png
tlc2.png (10.41 KiB) Viewed 5475 times
Now the problem is that I don't know how to modify the quoted code to get the label on the right or on the left according to the page number, as your code does.

Can you help me again?

Thanks,
Nino
Window + MiKTeX 2.7
Ubuntu + TexLive + Kile
Xandros (eeePC) + TeTex + TexMaker
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Re: Examples in LaTeX Companion

Post by gmedina »

Hi Nino,

I will need some minimal compilable code, showing all the settings for the new environment that you defined.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
NinV
Posts: 69
Joined: Sun Oct 05, 2008 12:57 pm

Examples in LaTeX Companion

Post by NinV »

Sorry! Here it is a minimal compilable code:

Code: Select all

\documentclass[10pt,a4paper,twoside,cp1256,utf8x]{book}
\usepackage[thref]{ntheorem}
\makeatletter
\renewtheoremstyle{marginbreak}%
  {\item[
    \rlap{
          \vbox{
                \hbox{
                      \theorem@headerfont
                      \llap{\fbox{##1 ##2}}
                      \hskip
                      \labelsep
                      \relax
                      \theorem@separator}
                \hbox{\strut}
                }
          }
         ]
  }
  {\item[\rlap{\vbox{\hbox{\theorem@headerfont
    \llap{\fbox{##1 ##2}}\hskip\labelsep\relax\
    ##3\theorem@separator}\hbox{\strut}}}]}
\makeatother

\theoremstyle{marginbreak}
%\theoremstyle{plain}
\theoremheaderfont{\normalfont\bfseries}\theorembodyfont{\slshape}
\theoremsymbol{\ensuremath{\clubsuit}}
\theoremseparator{:}
\newtheorem{reg}{Ex.}

\begin{document}

\chapter{Test chapter one}
\section{Test section one one}
\begin{reg}[Test 01]\label{t01}
  Test example one
\end{reg}
\begin{reg}[Test 02]\label{t02}
  Test example two
\end{reg}
\section{Test section one two}
\begin{reg}[Test 03]\label{t03}
  Test example three
\end{reg}

\chapter{Test chapter two}
\section{Test section two one}
\begin{reg}[Test 04]\label{t04
  Test example four
\end{reg}

\end{document}
Window + MiKTeX 2.7
Ubuntu + TexLive + Kile
Xandros (eeePC) + TeTex + TexMaker
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Examples in LaTeX Companion

Post by gmedina »

An example of the modifications needed:

Code: Select all

\documentclass[10pt,a4paper,twoside]{book}
\usepackage[thref]{ntheorem}
\usepackage{ifthen}

\makeatletter
\newcounter{pl}
\renewtheoremstyle{marginbreak}%
  {\stepcounter{pl}\label{pl-\thepl}%
    \item[\rlap{\vbox{\hbox{\theorem@headerfont
               \llap{\fbox{##1 ##2}}\hskip\labelsep\relax
               \theorem@separator}\hbox{\strut}}}]
  }
  {\stepcounter{pl}\label{pl-\thepl}%
    \ifthenelse{\isodd{\pageref{pl-\thepl}}}
         {\item[\rlap{\vbox{\hbox{\theorem@headerfont
           \llap{\fbox{##1 ##2}}\hskip\labelsep\relax\
           ##3\theorem@separator}\hbox{\strut}}}]
         }
         {\item[\hskip1.7cm\llap{\vbox{\hbox{\theorem@headerfont
           \rlap{\hspace{1.1\textwidth}\fbox{##1 ##2}}\hskip\labelsep\relax\
           ##3\theorem@separator}\hbox{\strut}}}]\hskip-1.7cm%
         }
  }
\makeatother

\theoremstyle{marginbreak}
  \theoremheaderfont{\normalfont\bfseries}\theorembodyfont{\slshape}
  \theoremsymbol{\ensuremath{\clubsuit}}
  \theoremseparator{:}
  \newtheorem{reg}{Ex.}

\begin{document}

text text text text text text text text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text
\begin{reg}[Test 01]\label{t01}
  Test example one
\end{reg}

\newpage

text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
\begin{reg}[Test 02]\label{t02}
  Test example two
\end{reg}

\end{document}
Remarks: 1) The behaviour of the environment depends on the parity of the page number in which it will appear. In this other thread

Flushleft and flushright in margins (odd an even pages)?

you will find an explanation of the machanism used in my example code to control the aspect of the environment depending on the page parity.

2) Of course, my code is just an example and, as such, admits improvements; feel free to adapt it according to your needs.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
NinV
Posts: 69
Joined: Sun Oct 05, 2008 12:57 pm

Re: Examples in LaTeX Companion

Post by NinV »

Thank you very much, but your code prints the example number always on the right margin. I tried to modify it but I didn't succeed :(
Last edited by NinV on Thu Sep 03, 2009 8:12 am, edited 1 time in total.
Window + MiKTeX 2.7
Ubuntu + TexLive + Kile
Xandros (eeePC) + TeTex + TexMaker
Post Reply