TransWikia.com

Weird vertical spacing with custom tcolorbox and tabular environment

TeX - LaTeX Asked by Alex Eastman on December 11, 2020

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=footnotesizettfamily,
    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}
            Largetextbf{Web Canvas API Reference --- Lite}
            largetextbf{ 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

One Answer

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
}
newcommandCode[2]{begin{CODE}#1tcblower#2end{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
}
newcommandCode[2]{begin{CODE}#1tcblower#2end{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=footnotesizettfamily,
    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}
            Largetextbf{Web Canvas API Reference --- Lite}
            largetextbf{ 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:)

Correct answer by user30471 on December 11, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP