我试图让灰色codebox
与表格右侧的解释很好地对齐,但显然垂直对齐有点偏离,我不确定原因。
此外,如果解释跨越多行,问题似乎会更严重。理想情况下,我希望tcolorbox
左侧的解释位于右侧解释的垂直中间。
奖金:我可以在宏中放入一些东西吗methodt
,这样我就不必把\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}
\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}
答案1
我不确定为什么这些框没有对齐,但我会以不同的方式做这件事并使用sidebyside
彩色盒子. 如果你定义:
\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}}
然后你可以用以下方法替换你的方法部分
\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}
(我已经tabular
从methodt
环境中移除了环境)以产生:
完整代码如下:
\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}