答案1
由于您需要自动语法突出显示,我建议您使用 listings
或者minted
。下面我介绍使用前者的两种选择。
这是第一个选择;这个想法是listings
包和两个并排的minipage
;两个环境sass
并less
使用定义\lstnewenvironment
;由于语言没有预定义,我为示例提供了一个简单的虚拟定义:
代码:
\documentclass{article}
\usepackage{listings}
\usepackage{etoolbox}
\usepackage{bera}
% Definitions for the SASS language
\lstdefinelanguage{sass}
{
morekeywords={border,solid},
}
% Definitions for the LESS language
\lstdefinelanguage{less}
{
morekeywords={bordered,black},
}
% Common settings
\lstset{
basicstyle=\small\ttfamily,
columns=fullflexible,
keywordstyle=\bfseries
}
% Definition of the main environments
\lstnewenvironment{sass}[1][]
{\lstset{language=sass,linewidth=\linewidth,#1}}
{}
% Definition of the main environments
\lstnewenvironment{less}[1][]
{\lstset{language=less,linewidth=\linewidth,#1}}
{}
\BeforeBeginEnvironment{sass}
{\par\noindent\begin{minipage}{.5\linewidth}SASS}
\AfterEndEnvironment{sass}{\end{minipage}}
\BeforeBeginEnvironment{less}
{\begin{minipage}{.5\linewidth}LESS}
\AfterEndEnvironment{less}{\end{minipage}}
\begin{document}
\begin{sass}
.bordered(@width: 2px) {
border: @width solid black;
}
#menu a {
.bordered(4px);
}
\end{sass}%
\begin{less}
@mixin bordered($width: 2px) {
border: $width solid black;
}
#menu a {
@include bordered(4px);
}
\end{less}
\end{document}
代码:
\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{filecontents}
\usepackage{bera}
\tcbuselibrary{listings}
% Definitions for the SASS language
\lstdefinelanguage{sass}
{
morekeywords={border},
}
% Definitions for the LESS language
\lstdefinelanguage{less}
{
morekeywords={bordered},
}
% Common settings
\lstset{
basicstyle=\small\ttfamily,
columns=fullflexible,
keywordstyle=\bfseries
}
% Just a snippet of LESS code for the example
\begin{filecontents*}{lessi.cd}
@mixin bordered($width: 2px) {
border: $width solid black;
} | }
#menu a {
@include bordered(4px);
}
\end{filecontents*}
% Definition of the main environment
\newtcblisting{lesssass}[2][]{
enhanced,
boxrule=0pt,
arc=0pt,
top=10pt,
listing options={language=sass},
colback=gray!5,
colframe=gray,
listing side comment,
comment={#2},
overlay={
\node[anchor=north west,inner ysep=4pt] at (frame.north west) (sa) {SASS};
\node[anchor=north west,inner ysep=4pt] at (frame.north) (le) {LESS};
\draw[gray,line width=0.5pt]
(frame.north west|-sa.south) -- (frame.north east|-sa.south);
},
#1
}
\begin{document}
\begin{lesssass}{\lstinputlisting[language=less]{lessi.cd}}
.bordered(@width: 2px) {
border: @width solid black;
}
#menu a {
.bordered(4px);
}
\end{lesssass}
\end{document}
评论
由于 LESS 和 SASS 不是中的预定义语言
listings
,因此您需要使用提供语言定义\lstdefinelanguage
;在我的示例代码中,我使用了简单的定义仅用于示例:% Definitions for the SASS language \lstdefinelanguage{sass} { morekeywords={border}, } % Definitions for the LESS language \lstdefinelanguage{less} { morekeywords={bordered}, }
在我的解决方案中,SASS 代码直接在您的文档中排版,在主环境中;LESS 代码必须存储在外部文件中(我在示例中使用模拟
filecontents
),并将使用输入\lstinputlistings
(参见示例代码)。主环境是
lesssass
;环境的内容是 SASS 代码(将排版到框的左侧);使用强制参数 和\lstinputlistings
,您可以编写 LESS 代码(将排版到框的右侧)。例如,文档是使用\begin{lesssass}{\lstinputlisting[language=less]{lessi.cd}} .bordered(@width: 2px) { border: @width solid black; } #menu a { .bordered(4px); } \end{lesssass}
使用以下文件
lessi.cd
:@mixin bordered($width: 2px) { border: $width solid black; } | } #menu a { @include bordered(4px); }