For theorems and a like I use mdframed package. For theorems, definitions I use standard design, meanwhile for examples and exercises I rather to have more exotic one ...
So far I succeed to make the following solution:
\documentclass[12pt,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\usepackage{showframe}
\usepackage[framemethod=tikz]{mdframed}
\usetikzlibrary{calc, positioning, shapes.geometric}
\tikzset{
node distance= 0mm and 3mm,
ws/.style={align=#1,
font=\large\bfseries\sffamily,
draw, thin, fill=white,
overlay,
}
}
\makeatletter
\mdfdefinestyle{example}%
{%
hidealllines=true,
skipabove=2\baselineskip, skipbelow=-1ex,
innertopmargin=0pt, innerbottommargin=0pt,%
middlelinewidth=5pt, linecolor=red,%
fontcolor=teal, font=\small\sffamily,%
%
settings={%\global\refstepcounter{example}
\ifodd\value{page}
\boolfalse{mdf@leftline}
\booltrue{mdf@rightline}
\else
\booltrue{mdf@leftline}
\boolfalse{mdf@rightline}
\fi},
innerrightmargin=\ifodd\value{page}0.5em\else 0.0em\fi, %rightmargin=7em,%
innerleftmargin =\ifodd\value{page}0.0em\else 0.5em\fi, %leftmargin =7em,%
singleextra={
\ifodd\value{page}
\node[ws=left,below right=of P] {\theexample};
\else
\node[ws=right,below left=of O |- P] {\theexample};
\fi
},%
}% end of mdfdefinestyle
\makeatother
\mdtheorem[style=example,
]{example}{Example}[chapter]
\mdtheorem[style=exercise,
]{exercise}{Exercise}[chapter]
\begin{document}
\chapter{test}
\begin{mdframed}[style=example]
This is produced by \verb+\begin{mdframed}[style=example] ... \end{mdframed}+
\end{mdframed}
xxxxxxxxx
\begin{example}
This is produced by \verb+\begin{example} ... \end{example}+
\end{example}
\newpage
\lipsum[2]
\begin{mdframed}[style=example]
\lipsum[2]
\end{mdframed}
\begin{example}
\lipsum[2]
\end{example}
\end{document}
I like to have the "Example " instead of top of an example tex rather in the box with now with number on the right side of text (or on the left side, if page is even). Is this possible?
Any improvement of my code is also welcome!
Addendum:
Meanwhile I gave-up to manage solution with mdframed and define equivalent environments with tcolorbox (as suggested in early comments). With it the solution is:
\documentclass[12pt,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[many]{tcolorbox}
\usetikzlibrary{positioning}
\usepackage{etoolbox}
\usepackage{lipsum}
\usepackage{showframe}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXERCISES %
%%==============================================================%
\newcounter{task}
\AtBeginEnvironment{task}{\linespread{.9}\selectfont}
%---------------------------------------------------------------%
\newtcolorbox[use counter=task,
number within=section,
]{task}%
{enhanced jigsaw,
breakable,
toggle left and right,
boxrule=0mm,
colback=white, colframe=red,
top=0mm,bottom=1mm,left=1mm,right=1mm,
arc=0mm,
borderline={0.5pt}{0pt}{red, sharp corners},
fontupper=\normalsize\sffamily, fontlower=\normalsize\sffamily,
rightrule=1mm,
overlay={\renewcommand\thetcbcounter{\thesection-\arabic{\tcbcounter}}%
\tcbifoddpage{\node[font=\large\sffamily, text=red, inner sep=0mm,
below right=0mm and \marginparsep] at (frame.north east) {Vaja
\thetcbcounter};}
{\node[font=\large\sffamily, text=red, inner sep=0mm,
below left=0mm and \marginparsep] at (frame.north west) {Vaja
\thetcbcounter};}
}%end of overlay
}% end of task
\begin{document}
\chapter{Chapter title}
\section{Test of "task" environment defined\newline
with use of "tcolorbox" package}
I looking for equivalent solution with package \verb+mdframed+, so far my attempts was unsuccessful.
\begin{task}
This is produced by \verb+\begin{task}{}{} ... \end{task}+ defined with \verb+tcolorbox+
\end{task}
\lipsum[2]
\begin{task}
\lipsum[1-2]
\end{task}
\end{document}
which gives:
Result is almost satisfactory ... in it I miss identification if the "Vaja ..." is on the second page or not. if it is, I like to add (in second line) "nadaljevanje" (continue), but this I left for time when I will finished text book, where I use this environment.
Now I testing of G. Bay solution. As is, it works, unfortunately without numbering, as I expected, however, with its integration in my collection mdframed based theorems, definitions etc, I still have problems.
In final solution I like to obtain similar solution of use as is used in other mdframed theorems, for example at definition:
\begin{definition}{Informacija}{info}
\index{informacije!definicija}
Informacija je enaka nezanesljivosti, ki jo nastop dogodka odstrani.
\end{definition}
Iz definicije \ref{def:info} sledi:
So I sill looking for any suggestion.




tcolorboxalternatives? – Ignasi Mar 05 '16 at 16:10mdframedsolution, I will accepttcolorboxalternative :-). In this case I need to rewrote existing solution for othermdframedboxes that I unified design of my documents. It seems, thatmdframedis death-end way :-( – Zarko Mar 05 '16 at 16:28mdframedand now usetcolorboxfor my needs ... however, I will try to elaborate question (in late evening) again. Shortly: how to build framed boxes which is aware for being on odd or even page even their part broken if box lay on two pages. – Zarko May 22 '16 at 15:47