2

I'm trying to put two code examples in a tcolorbox side by side. The problem is that I am using the alltt environment a derivative of the verbatim environment.

Here is my code:

\begin{tcolorbox}[sidebyside,title=Exemple]
    On souhaite créer trois variables \verb~a~, \verb~b~ et \verb~c~ avec des chiffres puis faire des opérations dessus.
    \begin{alltt}
    {\color{green!50!black!}a} ← 2       (la variable a vaut 2)
    {\color{green!50!black!}b} ← 3       (la variable b vaut 3)
    {\color{green!50!black!}c} ← {\color{green!50!black!}a} + {\color{green!50!black!}b}
    {\color{red}afficher} {\color{green!50!black!}c}
    \(\Longrightarrow\) 5
    \end{alltt}
    \tcblower
    On souhaite créer deux variables \verb~prenom~ et \verb~nom~ avec du texte.
    \begin{alltt}
    {\color{green!50!black!}prenom} ← "Jean"
    {\color{green!50!black!}nom} ← "Jaurès"
    {\color{red}afficher {\color{green!50!black!}prenom} + {\color{green!50!black!}nom}}
    \(\Longrightarrow\) "Jean Jaurès"
    \end{alltt}
\end{tcolorbox}

And here is the result:

problem with a tcolorbox

As you can see the problem is that the second column doesn't start at the same level of the first one, it is lower. I think it is because of the verbatim environment (well the alltt) because when I delete it I don't have this problem.

0

1 Answer 1

3

You need sidebyside align=top.

\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage[many]{tcolorbox}
\usepackage{alltt}

\begin{document}

\begin{tcolorbox}[
  sidebyside,
  sidebyside align=top,
  title=Exemple
]
  On souhaite créer trois variables \verb~a~, \verb~b~ et \verb~c~ avec
  des chiffres puis faire des opérations dessus.
  \begin{alltt}
  {\color{green!50!black!}a} ← 2       (la variable a vaut 2)
  {\color{green!50!black!}b} ← 3       (la variable b vaut 3)
  {\color{green!50!black!}c} ← {\color{green!50!black!}a} + {\color{green!50!black!}b}
  {\color{red}afficher} {\color{green!50!black!}c}
  \(\Longrightarrow\) 5
  \end{alltt}
  \tcblower
  On souhaite créer deux variables \verb~prenom~ et \verb~nom~ avec du texte.
  \begin{alltt}
  {\color{green!50!black!}prenom} ← "Jean"
  {\color{green!50!black!}nom} ← "Jaurès"
  {\color{red}afficher {\color{green!50!black!}prenom} + {\color{green!50!black!}nom}}
  \(\Longrightarrow\) "Jean Jaurès"
  \end{alltt}
\end{tcolorbox}

\end{document}

enter image description here

Of course, a better code would be

\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage[many]{tcolorbox}
\usepackage{alltt}

\colorlet{one}{green!50!black!}
\colorlet{two}{red}
\newcommand{\OO}[1]{\textcolor{one}{#1}}
\newcommand{\TT}[1]{\textcolor{two}{#1}}

\begin{document}

\begin{tcolorbox}[
  sidebyside,
  sidebyside align=top,
  title=Exemple
]
  On souhaite créer trois variables \verb~a~, \verb~b~ et \verb~c~ avec
  des chiffres puis faire des opérations dessus.
  \begin{alltt}
  \OO{a} ← 2       (la variable a vaut 2)
  \OO{b} ← 3       (la variable b vaut 3)
  \OO{c} ← \OO{a} + \OO{b}
  \TT{afficher} \OO{c}
  \(\Longrightarrow\) 5
  \end{alltt}
  \tcblower
  On souhaite créer deux variables \verb~prenom~ et \verb~nom~ avec du texte.
  \begin{alltt}
  \OO{prenom} ← "Jean"
  \OO{nom} ← "Jaurès"
  \TT{afficher} \OO{prenom} + \OO{nom}
  \(\Longrightarrow\) "Jean Jaurès"
  \end{alltt}
\end{tcolorbox}

\end{document}

Rename \OO, \TT, one and two to be more semantic in your context.

3
  • Yes thanks. Noticed my mistake, I was using valign=top which didn't work.
    – Nicryc
    Nov 11, 2021 at 23:01
  • @Nicryc I added some advice on how to improve your code.
    – egreg
    Nov 11, 2021 at 23:02
  • Oh thanks, will use it.
    – Nicryc
    Nov 11, 2021 at 23:03

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.