4

I need to add piece of source code in LESS and SASS. Piece of code is divided into two columns. First column is source code in Less and second column in Sass, something like on this page, but without borders.

I know about verbatim, but I would like to know if there is something better to save more space (because of two columns) and LESS and SASS syntax highlighting (I dont need color, bold and italic is enough).

6

Since you want automatic syntax highlighting, I'd suggest you to use either listings or minted. Below I present two options using the former.

Here's a first option; the idea is to the listings package and two side-by-side minipages; two environments sass and less are defined using \lstnewenvironment; since the languages are not predefined, I gave a simple dummy definition for the example:

enter image description here

The code:

\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}

Here's another option using the powerful tcolorbox package and its interaction with listings:

enter image description here

The code:

\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}

Remarks

  • Since LESS and SASS are not predefined languages in listings, you'll need to provide the language definitions using \lstdefinelanguage; in my example code I used to simple definitions just for the example:

    % Definitions for the SASS language
    \lstdefinelanguage{sass}
    {
      morekeywords={border},
    }
    
    % Definitions for the LESS language
    \lstdefinelanguage{less}
    {
      morekeywords={bordered},
    }
    
  • In my solution, the SASS code is typeset directly in your document, inside the main environment; the LESS code has to be stored in an external file (which I simulated in my example using filecontents) and will be input using \lstinputlistings (see the example code).

  • The main environment is lesssass; the content of the environment is the SASS code (which will be typeset to the left of the box); using the mandatory argument and \lstinputlistings, you can write the LESS code (to be typeset to the right of the box). For example, the document was produced using

    \begin{lesssass}{\lstinputlisting[language=less]{lessi.cd}}
    .bordered(@width: 2px) {
      border: @width solid black;
    } 
    #menu a {
      .bordered(4px);
    }
    \end{lesssass}
    

    using the following file lessi.cd:

    @mixin bordered($width: 2px) {
      border: $width solid black;
    }                                 | }
    #menu a {
      @include bordered(4px);
    }
    

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.