I'd like to visually divide a listing containing a hex dump into regions and provide a label for them.
Here's what I got so far:
The code for this uses tcolorbox for the overlays and textpos to make them show up on top of the right area.
While I'm content with the outcome (although I'm open to different visualizations to mark the hex dump's regions), I'm looking for a way to make the overlays more robust. Currently, the overlays are positioned absolutely and thus require quite some trial and error to get right. This is fragile: If I add some text to the overlay labels or change the underlying hex dump, the overlays won't line up anymore.
Here's the code, I compiled it successfully with lualatex
1.12.0 on Arch Linux:
\documentclass{beamer}
\usepackage{listings}
\usepackage[most]{tcolorbox}
\usepackage[absolute,overlay]{textpos}
\definecolor{codebackground}{HTML}{EFF0F1}
\definecolor{codeforeground}{HTML}{34383C}
\newcommand{\code}[1]{
\colorbox{codebackground}{\textcolor{codeforeground}{\texttt{{#1}}}}
}
% Hex dump background color
\definecolor{brightyellow}{HTML}{F7F5CD}
% Colors for overlays
\definecolor{fuchsia}{HTML}{8661C1}
\definecolor{pearl}{HTML}{E8E1D5}
\definecolor{darkgreen}{HTML}{204040}
\definecolor{lightolive}{HTML}{DADDC2}
\definecolor{lightlime}{HTML}{D6E6CC}
\definecolor{rose}{HTML}{F15BB5}
\definecolor{softpink}{HTML}{F7E0D3}
\begin{document}
\defverbatim[colored]\lstHexdumpFatNoFile{
\begin{lstlisting}[ backgroundcolor=\color{brightyellow}, basicstyle=\tiny\ttfamily\color{codeforeground}, ]
00000000 eb 3c 90 6d 6b 66 73 2e 66 61 74 00 02 04 04 00 |.<.mkfs.fat.....|
00000010 02 00 02 00 50 f8 14 00 20 00 02 00 00 00 00 00 |....P... .......|
00000020 00 00 00 00 80 00 29 10 61 b9 be 4d 59 20 44 52 |......).a..MY DR|
00000030 49 56 45 20 20 20 46 41 54 31 36 20 20 20 0e 1f |IVE FAT16 ..|
00000040 be 5b 7c ac 22 c0 74 0b 56 b4 0e bb 07 00 cd 10 |.[|.".t.V.......|
00000050 5e eb f0 32 e4 cd 16 cd 19 eb fe 54 68 69 73 20 |^..2.......This |
00000060 69 73 20 6e 6f 74 20 61 20 62 6f 6f 74 61 62 6c |is not a bootabl|
00000070 65 20 64 69 73 6b 2e 20 20 50 6c 65 61 73 65 20 |e disk. Please |
00000080 69 6e 73 65 72 74 20 61 20 62 6f 6f 74 61 62 6c |insert a bootabl|
00000090 65 20 66 6c 6f 70 70 79 20 61 6e 64 0d 0a 70 72 |e floppy and..pr|
000000a0 65 73 73 20 61 6e 79 20 6b 65 79 20 74 6f 20 74 |ess any key to t|
000000b0 72 79 20 61 67 61 69 6e 20 2e 2e 2e 20 0d 0a 00 |ry again ... ...|
000000c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
00000200 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00000800 f8 ff ff ff 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000810 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00003000 f8 ff ff ff 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00003010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00005800 4d 59 20 44 52 49 56 45 20 20 20 08 00 00 27 b1 |MY DRIVE ...'.|
00005810 8e 52 8e 52 00 00 27 b1 8e 52 00 00 00 00 00 00 |.R.R..'..R......|
00005820 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00a00000
\end{lstlisting}
}
\def\reservedSectorsRegionOverlay{
\begin{textblock*}{10cm}(0.9cm, 1.8cm)
\begin{tcolorbox}[enhanced jigsaw, opacityback=0.35, top=0mm,bottom=4.0mm, boxsep=14mm, colframe=fuchsia, colback=fuchsia!40] \begin{tcolorbox}[enhanced jigsaw, colback=pearl,opacityback=0.65, boxrule=0.0pt, left=0mm, right=0mm, boxsep=0mm, halign=center, leftright skip=1cm, colupper=codeforeground]Reserved Sectors Region\end{tcolorbox}\end{tcolorbox}
\end{textblock*}
}
\def\firstFatOverlay{
\begin{textblock*}{10cm}(0.9cm, 5.95cm)
\begin{tcolorbox}[enhanced jigsaw, opacityback=0.35, top=0mm,bottom=0mm, boxsep=1mm, colframe=darkgreen, colback=darkgreen!40] \begin{tcolorbox}[enhanced jigsaw, colback=lightolive, opacityback=0.65, boxrule=0.0pt, left=0mm, right=0mm, top=0.5mm, bottom=0.5mm, boxsep=0mm, halign=center, leftright skip=3.2cm, colupper=codeforeground]\small{First FAT}\end{tcolorbox}\end{tcolorbox}
\end{textblock*}
}
\def\secondFatOverlay{
\begin{textblock*}{10cm}(0.9cm, 6.67cm)
\begin{tcolorbox}[enhanced jigsaw, opacityback=0.35, top=0mm,bottom=0mm, boxsep=1mm, colframe=teal, colback=teal!40 ] \begin{tcolorbox}[enhanced jigsaw, colback=lightlime, opacityback=0.65, boxrule=0.0pt, left=0mm, right=0mm, top=0.5mm, bottom=0.5mm, boxsep=0mm, halign=center, leftright skip=3.2cm,colupper=codeforeground]\small{Second FAT}\end{tcolorbox}\end{tcolorbox}
\end{textblock*}
}
\def\rootDirectoryOverlay{
\begin{textblock*}{10cm}(0.9cm, 7.45cm)
\begin{tcolorbox}[enhanced jigsaw, opacityback=0.35, top=1mm,bottom=0mm, boxsep=1mm, colframe=rose, colback=rose!40] \begin{tcolorbox}[enhanced jigsaw, colback=softpink, opacityback=0.65, boxrule=0.0pt, left=0mm, right=0mm, top=0.5mm, bottom=0.5mm, boxsep=0mm, halign=center, leftright skip=3.2cm, colupper=codeforeground]\small{Root Directory}\end{tcolorbox}\end{tcolorbox}
\end{textblock*}
}
\frame{
\frametitle{Brand New FAT16}
\framesubtitle{Viewed with \code{hexdump -C}}
\only<1-> \lstHexdumpFatNoFile
\only<2-> \reservedSectorsRegionOverlay
\only<3-> \firstFatOverlay
\only<4-> \secondFatOverlay
\only<5-> \rootDirectoryOverlay
}
\end{document}
absolute
mode? The defaultrelative
mode sounds like a more natural fit to this case. Also, recall that textpos has\TPVertModule
and\TPHorizModule
to define the ‘units’ of vertical and horizontal spacing. If the former were the line-pitch of the underlying image, you might make things easier for yourself.tikzmark
to mark spots. The problem is that you have to declare one ascii char to be the escape, and then you need to escape it manually when it occurs in the second column... Hmmm...textpos
! I read the following in the documentation and didn't try relative mode: "The Beamer package’s background frame overlays the textpos material, unless the textpos & beamer [overlay] option is used to tell textpos to delay it. Therefore, you should use the [overlay,absolute] options with Beamer"absolute
option with beamer (I've little experience with that, and may have been passing on some other user's advice). It might be that usingabsolute
is good general advice for beamer, but not in fact necessary. Question: should I amend the manual's advice here?