Text FormattingAutomatic paragraph coloring pattern

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Automatic paragraph coloring pattern

Post by gmedina »

Some time ago a challenge was posed in another forum[1] in this thread and I haven't been able to design an acceptable solution to the proposed problem. The idea is to define a command \selstudy with two mandatory arguments; the code

Code: Select all

\selfstudy{Exercises 1 and 4}{Review the information from Exercise 5. Make a brief recording answering this question: Are the requirements for a college degree in your country similar to those in the United States? Explain the similarities and differences.}
should produce the outcome shown in tha attached image.

The difficult part (for me) is to achieve the color pattern for the text. Any ideas would be most welcome!

[1] http://foro.migui.com/
Attachments
desafio.jpg
desafio.jpg (74.77 KiB) Viewed 2048 times
1,1,2,3,5,8,13,21,34,55,89,144,233,...

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

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

Automatic paragraph coloring pattern

Post by gmedina »

Just an update. A few days ago (February the 14th) I posted a question about this problem in this thread in comp.text.tex; Philipp Stephani gave me the hint that I needed to finally build the required command.

Here I post the final solution to the challenge and an example; the code contains some comments (in Spanish):

Code: Select all

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{calc,shapes}

% caja auxiliar (para guardar cada línea en \ponerlineas)
\newsavebox\linebox

% contador para el número de líneas en el párrafo
% al que se aplica \ponerlineas
\newcounter{numlin}

% macro que disecta párrafos en sus líneas constituyentes
% para (en este caso) agregar una línea blanca entre renglones
\newcommand*\ponerlineas{%
 \setcounter{numlin}{0}
 \setlength\textwidth{14.8cm}% 
 \setbox\linebox\lastbox 
 \ifvoid\linebox\medskip\else 
   \unskip 
   \unpenalty 
   {\ponerlineas}% 
   \hspace*{3cm}\box\linebox\hspace*{-3cm}
   \smallskip\stepcounter{numlin}
   \color{white}{\hrule width\textwidth height1pt} 
   \smallskip
 \fi
} 

% el ícono para el casete
\newcommand*\casete{%
\begin{tikzpicture}
 \node [matrix,fill=black,inner sep=2pt,ampersand replacement=\&]
 {%
   \draw [draw=none,fill=white] (-2pt,0) circle (1.5pt); \& 
   \draw [fill=black] (0,0) circle (1.5pt); \&
   \draw [draw=none,fill=white] (0,0) circle (1.5pt);\\
  };
\end{tikzpicture}
}

% el ícono para los audífonos
\newcommand*\audifonos{%
\begin{tikzpicture}[node distance=5pt]
 \node (b) [fill=black,minimum size=4pt,circle,inner sep=0pt] {};
 \node (c) [fill=black,minimum size=4pt,circle,inner sep=0pt,left of=b] {};
\draw [very thick] (b) .. controls +(60:9pt) and +(120:9pt) .. (c);
\end{tikzpicture}
}

% longitud a usar para adicionar espacio vertical luego del tikzpicture
\newlength\mylen

% el comando principal
\newcommand*\miejercicio[2]{%
\setlength\mylen{0pt}
\begin{tikzpicture}[remember picture,overlay]
 \node[xshift=-1.24in,anchor=north west,inner sep=0pt] (a)  
 {\setlength\fboxsep{0pt}%
   \colorbox{gray!50}{%
     \hspace*{-3pt}%
     \vbox{%
       \mbox{\ }\\% 
       \llap{\makebox[23pt][c]{\audifonos}}\textbf{First listen to:}\\%
         \parshape 1 2cm 10cm %
         \makebox[12pt][l]{\textbullet}#1\\% 
       \llap{\makebox[23pt][c]{\casete}}\textbf{Now record them.}\\%
         #2\\% 
       \phantom{A}    
       \par
       \ponerlineas
     }%
   }%
};
\end{tikzpicture}
\begin{tikzpicture}[remember picture,overlay]
 \draw [fill=white,draw=none] 
   ($(a.north west)$) -- +(2cm,0cm) -- +(0cm,-1.95cm);
 \draw  [fill=black!75,draw=none] 
   ($(a.north west)+(2cm,0cm)$) -- +(0,-1.95cm) --  +(-2cm,-1.95cm) -- 
   node [below,rotate=44,xshift=2pt,yshift=1pt] 
   {%
     \textcolor{white}{\fontsize{9}{10}\selectfont\bfseries\sffamily SELF-STUDY}
   } 
   ($(a.north west)+(2cm,0cm)$) ;
\end{tikzpicture}\par
\addtocounter{numlin}{3}
\addtolength\mylen{\thenumlin\baselineskip}
\addtolength\mylen{\thenumlin pt}
\vspace*{\mylen}
}

\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

\miejercicio{Exercises 1 and 4.}{Review the information from Exercise 5. Make a brief recording answering this question: Are the requirements for a college degree in your country similar to those in the United States? Explain the similarities and differences.}

text text text text text text text text text text text text text text text text text text text text text text text text

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Post Reply