1

I'm trying to get the gray codebox's to line up nicely with their explanations on the right side of the table, but clearly the vertical alignment is just a little off and I'm not sure why.

Additionally, if the explanation spans multiple lines, it seems like the problem is worse. Ideally, I'd like to have the tcolorbox on the left in the vertical middle of the explanation on the right.

Bonus: Is there something I can put in my methodt macro so I don't have to put \code{} around every entry on the left?

Here's an reproducible example, and below that is an image:

\documentclass{article}

\usepackage{tabularx}  % Allows easy wrapping of text in tables
\usepackage{setspace}  % Allows setting line spacing throughout document
\usepackage{listings}  % Used for code sections
\usepackage{color}     % Used to define colors
\usepackage[a4paper, total={6in, 8in}]{geometry}  % Used to set page dimensions
\geometry{             % Setting margins
    top = 1in,
    bottom = 1in
}
\usepackage{hyperref}  % Allows blue styling of links and clickable table of contents
\hypersetup{
    colorlinks,
    linkcolor = blue
}

% Following allows inline code
\usepackage{tcolorbox}
\definecolor{dark-gray}{gray}{0.85}
\definecolor{light-gray}{gray}{.95}
%\newtcolorbox[〈init options〉]{〈name〉}[〈number〉][〈default〉]{〈options〉}

\newtcolorbox{codebox}{
    colback=light-gray,
    colframe=white
}
\newcommand{\code}[1] {
    \begin{codebox}
    {#1}
    \end{codebox}
}

\newtcolorbox{exbox} {
    colback=white,
    colframe=white
}
\newcommand{\ex}[1] {
    \begin{exbox}
        {#1}
    \end{exbox}
'}


%\renewcommand*\arraystretch{1.085}  % Setting vertical spacing in tables

\newcommand{\secspace}{\vspace{ .25in }}  % Separator between sections
\newcommand{\secline}{\vspace{ 6pt } \hrule width 2in \vspace{ 6pt }}  % Horizontal line between section title and content
\newenvironment{methodt}[1]  % Table for use with methods. Left side is method, right side is description
    {%
        \subsection{ #1 }
        \secline
        \begin{tabular}{ p{3.0in}p{2.5in} }
    }
    {%
        \end{tabular}
        \secspace
    }

% Defining colors for use in code segments
\definecolor{lightgray}{rgb}{.95,.95,.95}
\definecolor{darkgray}{rgb}{.4,.4,.4}
\definecolor{purple}{rgb}{0.65, 0.12, 0.82}

% Defining the JavaScript language
\lstdefinelanguage{JavaScript}{
    keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break},
    keywordstyle=\color{blue}\bfseries,
    ndkeywords={class, export, boolean, throw, implements, import, this},
    ndkeywordstyle=\color{darkgray}\bfseries,
    identifierstyle=\color{black},
    sensitive=false,
    comment=[l]{//},
    morecomment=[s]{/*}{*/},
    commentstyle=\color{purple}\ttfamily,
    stringstyle=\color{red}\ttfamily,
    morestring=[b]',
    morestring=[b]"
}

% Setting properties of code blocks
\lstset{
    language=JavaScript,
    backgroundcolor=\color{lightgray},
    extendedchars=true,
    basicstyle=\footnotesize\ttfamily,
    showstringspaces=false,
    showspaces=false,
    numbers=none,
    numberstyle=\footnotesize,
    numbersep=9pt,
    tabsize=2,
    breaklines=true,
    showtabs=false,
    captionpos=b
}

\begin{document}
    \setstretch{1.1}  % Set line spacing across document
    
    \begin{titlepage}
        \vspace*{\stretch{1.0}}
        \begin{center}
            \Large\textbf{Web Canvas API Reference --- Lite}\\
            \large\textbf{ Alex Eastman }
        \end{center}
        \vspace*{\stretch{2.0}}
    \end{titlepage}
    \clearpage
    
    \begin{flushleft}
        \tableofcontents
        \clearpage
        
        \section{Methods} \label{methods}
        
        % Rectangles
        \begin{methodt}{Rectangles}
            \code{rect(x, y, width, height)} & A rectangle whose top-left corner is at (x, y); Pen automatically moved\\
            \code{fillRect(x, y, width, height)} & A solid rectangle\\
            \code{strokeRect(x, y, width, height)} & An outline rectangle\\
            \code{clearRect(x, y, width, height)} & Clear a portion or all of a rectangle   
        \end{methodt}
    
\end{flushleft}
\end{document}

Image of Problem

1 Answer 1

0

I am not sure why these boxes are not aligning but I would do this differently and use a sidebyside tcolorbox. If you define:

\newtcolorbox{CODE}{
    sidebyside,
    sidebyside align=top,
    colback=light-gray,
    colframe=white,
    bicolor,
    colbacklower=white,
    righthand width=2.5in
}
\newcommand\Code[2]{\begin{CODE}#1\tcblower#2\end{CODE}}

Then you can replace your method section with

    \begin{methodt}{Rectangles}
      \Code{rect(x, y, width, height)}{A rectangle whose top-left corner is at (x, y); Pen automatically moved}
      \Code{fillRect(x, y, width, height)}{A solid rectangle}
      \Code{strokeRect(x, y, width, height)}{An outline rectangle}
      \Code{clearRect(x, y, width, height)}{Clear a portion or all of a rectangle}
    \end{methodt}

(I have removed the tabular environment from the methodt environment) to produce:

enter image description here

Here is the full code:

\documentclass{article}

\usepackage{tabularx}  % Allows easy wrapping of text in tables
\usepackage{setspace}  % Allows setting line spacing throughout document
\usepackage{listings}  % Used for code sections
\usepackage{color}     % Used to define colors
\usepackage[a4paper, total={6in, 8in}]{geometry}  % Used to set page dimensions
\geometry{             % Setting margins
    top = 1in,
    bottom = 1in
}
\usepackage{hyperref}  % Allows blue styling of links and clickable table of contents
\hypersetup{
    colorlinks,
    linkcolor = blue
}

% Following allows inline code
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\definecolor{dark-gray}{gray}{0.85}
\definecolor{light-gray}{gray}{.95}
%\newtcolorbox[〈init options〉]{〈name〉}[〈number〉][〈default〉]{〈options〉}

\newtcolorbox{CODE}{
    sidebyside,
    sidebyside align=top,
    colback=light-gray,
    colframe=white,
    bicolor,
    colbacklower=white,
    righthand width=2.5in
}
\newcommand\Code[2]{\begin{CODE}#1\tcblower#2\end{CODE}}

\newtcolorbox{exbox} {
    colback=white,
    colframe=white
}
\newcommand{\ex}[1] {%
    \begin{exbox}
        {#1}
    \end{exbox}%
'}


%\renewcommand*\arraystretch{1.085}  % Setting vertical spacing in tables

\newcommand{\secspace}{\vspace{ .25in }}  % Separator between sections
\newcommand{\secline}{\vspace{ 6pt } \hrule width 2in \vspace{ 6pt }}  % Horizontal line between section title and content
\newenvironment{methodt}[1]  % Table for use with methods. Left side is method, right side is description
    {%
        \subsection{ #1 }
        \secline
    }
    {%
        \secspace
    }

% Defining colors for use in code segments
\definecolor{lightgray}{rgb}{.95,.95,.95}
\definecolor{darkgray}{rgb}{.4,.4,.4}
\definecolor{purple}{rgb}{0.65, 0.12, 0.82}

% Defining the JavaScript language
\lstdefinelanguage{JavaScript}{
    keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break},
    keywordstyle=\color{blue}\bfseries,
    ndkeywords={class, export, boolean, throw, implements, import, this},
    ndkeywordstyle=\color{darkgray}\bfseries,
    identifierstyle=\color{black},
    sensitive=false,
    comment=[l]{//},
    morecomment=[s]{/*}{*/},
    commentstyle=\color{purple}\ttfamily,
    stringstyle=\color{red}\ttfamily,
    morestring=[b]',
    morestring=[b]"
}

% Setting properties of code blocks
\lstset{
    language=JavaScript,
    backgroundcolor=\color{lightgray},
    extendedchars=true,
    basicstyle=\footnotesize\ttfamily,
    showstringspaces=false,
    showspaces=false,
    numbers=none,
    numberstyle=\footnotesize,
    numbersep=9pt,
    tabsize=2,
    breaklines=true,
    showtabs=false,
    captionpos=b
}

\begin{document}
    \setstretch{1.1}  % Set line spacing across document

    \begin{titlepage}
        \vspace*{\stretch{1.0}}
        \begin{center}
            \Large\textbf{Web Canvas API Reference --- Lite}\\
            \large\textbf{ Alex Eastman }
        \end{center}
        \vspace*{\stretch{2.0}}
    \end{titlepage}
    \clearpage

    \begin{flushleft}
        \tableofcontents
        \clearpage

        \section{Methods} \label{methods}

        % Rectangles
        \begin{methodt}{Rectangles}
          \Code{rect(x, y, width, height)}{A rectangle whose top-left corner is at (x, y); Pen automatically moved}
          \Code{fillRect(x, y, width, height)}{A solid rectangle}
          \Code{strokeRect(x, y, width, height)}{An outline rectangle}
          \Code{clearRect(x, y, width, height)}{Clear a portion or all of a rectangle}
        \end{methodt}

\end{flushleft}

\end{document}

Btw, I would also the xcolor package package, instead of color...although I can not point to anywhere where this would make a difference:)

4
  • That’s a nice answer, thank you! Would ‘valign=center’ center the code on the left in the box? Jul 30, 2020 at 12:47
  • @AlexEastman I am not sure if it possible to centre one side and the other. You want to use sidebyside align -- see page 120 of the manual. Try sidebyside align=center seam.
    – user30471
    Jul 30, 2020 at 14:18
  • So I finally got around to trying this, and I'm getting an error that says "I do not know the key /tcb/bicolor", any idea why that would happen? Jul 31, 2020 at 1:01
  • I had to add \tcbuselibrary{skins} and then it worked. Thanks! Jul 31, 2020 at 1:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .