3

I'm trying to make a personal cheatsheet for keyboard shortcuts in (mostly) sublime text and openbox (linux window manager). I found this beautiful template:

https://gist.github.com/alexander-yakushev/c773543bf9a957749f79

that does almost everything I'd like it to, except that it would be very useful to be able to color the different key-sections in different colors. I think the key is in the definition of the key enviroment in line 73-94 here:

\NewEnviron{keys}[1]{
  % \begin{center}
  \smallskip
  \begin{tikzpicture}
      \rowcolors{1}{}{TableRow}
      \centering
      \node (tbl) [inner sep=0pt] {
        \begin{tabular}{p{4.2cm}p{3.25cm}}
          \rowcolor{TableHead}
          \multicolumn{2}{l}{\normalsize\textbf{\color{white}{#1}}}\parbox{0pt}{\rule{0pt}{0.3ex+\baselineskip}}\\
          \BODY
          \arrayrulecolor{TableHead}\specialrule{.17em}{0em}{.2em}
        \end{tabular}};
      \begin{pgfonlayer}{background}
        \draw[rounded corners=2pt,top color=TableHead,bottom color=TableHead, draw=white]
        ($(tbl.north west)-(0,-0.05)$) rectangle ($(tbl.north east)-(0.0,0.15)$);
        \draw[rounded corners=2pt,top color=TableHead,bottom color=TableHead, draw=white]
        ($(tbl.south west)-(0.0,-0.11)$) rectangle ($(tbl.south east)-(-0.0,-0.02)$);
      \end{pgfonlayer}
    \end{tikzpicture}
  % \end{center}
} 

I'm not good enough in latex to figure this out, however. Ideally, the key-command defined should have another argument so that:

  \begin{keys}{Glossary}

    Control (Ctrl) key  & \texttt{C} \\
    Alt (Meta) key      & \texttt{M} \\
    Shift key           & \texttt{S} \\
    Command (Super) key & \texttt{CMD} \\
    Function (Fn) key   & \texttt{fn} \\
    Home/end            & \texttt{fn-left / fn-right} \\
  \end{keys}

would be something like:

  \begin{keys}{Glossary}{purple}
    Control (Ctrl) key  & \texttt{C} \\
    Alt (Meta) key      & \texttt{M} \\
    Shift key           & \texttt{S} \\
    Command (Super) key & \texttt{CMD} \\
    Function (Fn) key   & \texttt{fn} \\
    Home/end            & \texttt{fn-left / fn-right} \\
  \end{keys}

so that the color of the section could be defined

below is an illustration of the pdf output from the template, annotated with color in order to make it even clearer.

enter image description here

Thank you for your time,

2 Answers 2

4

Here is a solution. I have added two arguments to the environment {keys}: one for the color and the title of the box and one for the rows in the tabular.

\documentclass{article}
\usepackage{calc}
\usepackage{array,booktabs}
\usepackage[usenames,dvipsnames,table]{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}


\pagestyle{empty} % Turn off header and footer

\NewDocumentEnvironment{keys}{mmm}
  {
     \smallskip
     \begin{tikzpicture}
         \rowcolors{1}{}{#3}
         \centering
         \node (tbl) [inner sep=0pt] \bgroup
          \begin{tabular}{p{4.2cm}p{3.25cm}}
             \rowcolor{#2}
             \multicolumn{2}{l}{\normalsize\textbf{\color{white}{#1}}}\parbox{0pt}{\rule{0pt}{0.3ex+\baselineskip}}\\
  }
  {         
           \arrayrulecolor{#2}\specialrule{.17em}{0em}{.2em}
         \end{tabular}\egroup ;
       \begin{pgfonlayer}{background}
         \draw[rounded corners=2pt,top color=#2,bottom color=#2, draw=white]
         ($(tbl.north west)-(0,-0.05)$) rectangle ($(tbl.north east)-(0.0,0.15)$);
         \draw[rounded corners=2pt,top color=#2,bottom color=#2, draw=white]
         ($(tbl.south west)-(0.0,-0.11)$) rectangle ($(tbl.south east)-(-0.0,-0.02)$);
       \end{pgfonlayer}
     \end{tikzpicture}
  }


\begin{document}

\footnotesize

Small text.

\begin{keys}{Glossary}{red}{red!15}
  Control (Ctrl) key  & \texttt{C} \\
  Alt (Meta) key      & \texttt{M} \\
  Shift key           & \texttt{S} \\
  Command (Super) key & \texttt{CMD} \\
  Function (Fn) key   & \texttt{fn} \\
  Home/end            & \texttt{fn-left / fn-right} \\
\end{keys}

\begin{keys}{Basic}{blue}{blue!15}
  Execute command by name          & \texttt{M-x} \\
  Cancel command                   & \texttt{C-g} \\
  Exit Emacs                       & \texttt{C-x C-c} \\
  Help: describe a key combination & \texttt{C-h k} \\
  Help: describe a function        & \texttt{C-h f} \\
\end{keys}

\end{document}

Output of the above code

2

Based on F. Pantignys answer, I modified the template so to include the multi-column layout of the original template. I also changed the xcolor from

\usepackage[usenames,dvipsnames,table]{xcolor}

to

\usepackage[usenames,x11names,table]{xcolor}

so as to have access to all the wonderful colors in x11names (listed in the package documentation but a better overview is this one)

here is the complete code with as small example

%% Copyright 2020 Alexander Yakushev
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Alexander Yakushev.
%
% This work consists of the files latex-cheatsheet-template.tex.

\documentclass[10pt,english,landscape]{article}
\usepackage{multicol}
\usepackage{calc}
\usepackage[landscape]{geometry}
\usepackage{color,graphicx,overpic}

\usepackage[T1]{fontenc}
\usepackage[bitstream-charter]{mathdesign}
\usepackage[utf8]{inputenc}
\usepackage{url}
\usepackage{amsfonts}
\usepackage{array,booktabs}
\usepackage{textcomp}
\usepackage[usenames,x11names,table]{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{tabularx}
\usepackage{multirow}
\usepackage{colortbl}
\usepackage{tikz}
\usepackage{environ}

\usetikzlibrary{calc}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\geometry{top=-0.5cm,left=1cm,right=1cm,bottom=1cm}

\pagestyle{empty} % Turn off header and footer

% \renewcommand\rmdefault{phv} % Arial
% \renewcommand\sfdefault{phv} % Arial

% Redefine section commands to use less space
\makeatletter
\renewcommand{\section}{\@startsection{section}{1}{0mm}%
  {-1ex plus -.5ex minus -.2ex}%
  {0.5ex plus .2ex}%x
  {\normalfont\large\bfseries}}
\renewcommand{\subsection}{\@startsection{subsection}{2}{0mm}%
  {-1explus -.5ex minus -.2ex}%
  {0.5ex plus .2ex}%
  {\normalfont\normalsize\bfseries}}
\renewcommand{\subsubsection}{\@startsection{subsubsection}{3}{0mm}%
  {-1ex plus -.5ex minus -.2ex}%
  {1ex plus .2ex}%
  {\normalfont\small\bfseries}}
\makeatother

\setcounter{secnumdepth}{0} % Don't print section numbers
\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt plus 0.5ex}

\definecolor{TableHead}{rgb}{0.353, 0.329, 0.667}
\definecolor{TableRow}{rgb}{0.816, 0.812, 0.902}




\NewDocumentEnvironment{keys}{mmm}
  {
     \smallskip
     \begin{tikzpicture}
         \rowcolors{1}{}{#3}
         \centering
         \node (tbl) [inner sep=0pt] \bgroup
          \begin{tabular}{p{4.2cm}p{3.25cm}}
             \rowcolor{#2}
             \multicolumn{2}{l}{\normalsize\textbf{\color{white}{#1}}}\parbox{0pt}{\rule{0pt}{0.3ex+\baselineskip}}\\
  }
  {         
           \arrayrulecolor{#2}\specialrule{.17em}{0em}{.2em}
         \end{tabular}\egroup ;
       \begin{pgfonlayer}{background}
         \draw[rounded corners=2pt,top color=#2,bottom color=#2, draw=white]
         ($(tbl.north west)-(0,-0.05)$) rectangle ($(tbl.north east)-(0.0,0.15)$);
         \draw[rounded corners=2pt,top color=#2,bottom color=#2, draw=white]
         ($(tbl.south west)-(0.0,-0.11)$) rectangle ($(tbl.south east)-(-0.0,-0.02)$);
       \end{pgfonlayer}
     \end{tikzpicture}
  }


\begin{document}


\raggedright\

\begin{center}
  \Large{\underline{My cheatsheet}}
\end{center}

\footnotesize

% change when one column isn't enough, up to 3 columns will work
\begin{multicols}{2} 

\centering\section{Sublime Text}

\begin{keys}{Move text}{DeepPink4}{DeepPink4!35}
    Control (Ctrl) key  & \texttt{C} \\
    Alt (Meta) key      & \texttt{M} \\
    Shift key           & \texttt{S} \\
    Command (Super) key & \texttt{CMD} \\
    Function (Fn) key   & \texttt{fn} \\
    Home/end            & \texttt{fn-left / fn-right} \\
\end{keys}

\begin{keys}{Basic}{DarkOrange1}{DarkOrange1!35}
    Execute command by name          & \texttt{M-x} \\
    Cancel command                   & \texttt{C-g} \\
    Exit Emacs                       & \texttt{C-x C-c} \\
    Help: describe a key combination & \texttt{C-h k} \\
    Help: describe a function        & \texttt{C-h f} \\
\end{keys}

\columnbreak\

\centering\section{Openbox}


\begin{keys}{Glossary}{SpringGreen4}{SpringGreen4!35}
    Control (Ctrl) key  & \texttt{C} \\
    Alt (Meta) key      & \texttt{M} \\
    Shift key           & \texttt{S} \\
    Command (Super) key & \texttt{CMD} \\
    Function (Fn) key   & \texttt{fn} \\
    Home/end            & \texttt{fn-left / fn-right} \\
\end{keys}



\end{multicols}

\end{document}

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.