7

I want to customization a sample exercises for a book. I like so much this style:

example style

I think that is possible with fbox and hrule, but I don't know how I can do it. No need to use fbox and hrule if there is another way to do.

EDIT: The bottom rule is here Creating a custom rule

UPDATE: I have made my example code with the help Dan and azetina. Thank you both and now I share the code:

\documentclass{article}
\usepackage{multicol}
\usepackage{colortbl}
\usepackage{blindtext}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{amssymb}


\addtolength\textwidth{2cm}
\addtolength\oddsidemargin{-1cm}
% 1. define its color
\definecolor{ejemplo}{RGB}{37,153,116}
% 2. define its counter, to be reset with each section
\newcounter{ejemplo}[section]
\renewcommand\theejemplo{\mdseries\normalsize\thesection.\arabic{ejemplo}}
% 3. define the environment. Its argument is the name of the example.
\makeatletter
\newenvironment{ejemplo}[1]{%
    \bigbreak
    \refstepcounter{ejemplo}
    \par%
% (a) heading
    \noindent{\color{ejemplo}\large\sffamily
    {\setlength{\extrarowheight}{5pt}\begin{tabular}{ll@{}}%
    \hline
        \cellcolor{ejemplo}\color{white}%
        \textmd{\normalsize Ejemplo} \theejemplo&\bfseries #1%
    \end{tabular}}}\par
% (b) space after and no indentation after
    \begin{multicols}{2}
        \vspace{1em}%
        \@afterindentfalse
        \@afterheading
}   {\end{multicols}
% (c) place the custom rule
    \noindent{\color{ejemplo}%
    \parbox{\linewidth}{%
        \hrulefill\rule{2em}{0.4em}}%
    }\par%
    }
% The solution
\newcommand\solucion{
    \par\vspace{1em}
    \noindent
    \colorbox{ejemplo}{\textsc{\MakeLowercase{\sffamily\mdseries\color{white}Soluci\'on}}}%
    \par\vspace{1em}
    \@afterindentfalse
    \@afterheading
}
\makeatother

And here the result:

example

  • Do you want to create an environment to replicate the example in the diagram or just the title of each example? – azetina Dec 6 '13 at 15:32
  • @azetina - I only want the title and footer, but if is possible all environment... I really appreciate it – Kiyoshi Dec 6 '13 at 16:06
  • Have you contemplated on using TikZ? – azetina Dec 6 '13 at 17:25
  • @azetina - No, but if with TikZ is possible I will use it – Kiyoshi Dec 6 '13 at 17:33
7

The Ejemplo part looks like it was set with colortbl and the Soluci\'on part with \colorbox Here is some code to produce those effects. Normally one would code examples with \newtheorem and one would have to go to some effort to make this automatic. But it is late, and I will leave that until later (or someone else can step in). I didn't try to match the color and just went with blue. One can, of course change colors, fonts, and sizes to suit ones tastes:

\documentclass{article}
\usepackage{colortbl}
\begin{document}
Some text.

\bigskip
\noindent{\color{blue}\Large\sffamily
\begin{tabular}{ll@{}}
\hline
\cellcolor{blue}\color{white}Ejemplo 37.12&\bfseries Choque Relativista
\end{tabular}}

\bigskip
Statement of example.

\bigskip
\noindent \colorbox{blue}{\sffamily\bfseries\color{white}SOLUCI\'ON}

\bigskip
Description of solution.

\end{document}

output

Here's a more complete example, coded as an ejemplo environment.

\documentclass{article}
\usepackage{colortbl}
\usepackage{multicol}
\usepackage{blindtext}
%
\addtolength\textwidth{2cm}
\addtolength\oddsidemargin{-1cm}
%
% Prepare an ejemplo environment
% 1. define its color
\definecolor{ejemplo}{rgb}{0.1,0.5,0.4}
% 2. define its counter, to be reset with each section
\newcounter{ejemplo}[section]
\renewcommand\theejemplo{\thesection.\arabic{ejemplo}}
% 3. Define the lengths of the rule that follows
\newlength\ejemplorulei
\newlength\ejemploruleii
\setlength\ejemploruleii{4em}
\setlength\ejemplorulei\textwidth
\addtolength\ejemplorulei{-\ejemploruleii}
% 4. define the environment. Its argument is the name of the
%    example.
\makeatletter
\newenvironment{ejemplo}[1]
{%
  \refstepcounter{ejemplo}
  \begin{multicols}{2}%
  %    (a) heading
  \noindent{\color{ejemplo}\large\sffamily
  \begin{tabular}{ll@{}}%
  \hline
    \cellcolor{ejemplo}\color{white}Ejemplo \theejemplo&\bfseries #1%
  \end{tabular}}\par
  %    (b) space after and no indentation after
  \vspace{1em}%
  \@afterindentfalse
  \@afterheading
}{\end{multicols}
  %    (c) place the custom rule
  \noindent{\color{ejemplo}%
  \rule[2ex]{\ejemplorulei}{.4pt}%
  \rule[2ex]{\ejemploruleii}{1.2pt}}\par
}
\newcommand\solucion{% The solution
  \par\vspace{1em}
  \noindent
  \colorbox{ejemplo}{\sffamily\bfseries\color{white}SOLUCI\'ON}%
  \par\vspace{1em}
  \@afterindentfalse
  \@afterheading
}
\makeatother
\begin{document}

\section{Start of section}
\blindtext
\begin{ejemplo}{Choque Relativista}
\blindtext[1]
\solucion
  \blindtext[2]
\end{ejemplo}

\end{document}

more complete ejemplo

  • It looks good! Just missing the bottom but I do not know how to do the lower right box. If possible make the example in two columns? – Kiyoshi Dec 7 '13 at 9:47
  • Sorry, I didn't even see the line at the bottom. It's just two rules. I'll be adding a more-or-less complete solution in a bit – Dan Dec 7 '13 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.