2

I'm trying to define a new language cli-like interface, but I can not figure out how the use the delimiter to match my prompt.

I want to apply tree different styles to the text. In the example below I have underlined each char with a ~, - or = to signal what style group they should belong to.

# configure terminal
~ ==================
(config)# json notification host a
--------~ ========================
(something-something)# url http://10.99.10.1:8000/path
---------------------~ ===============================

I have been experimenting with this:

\documentclass{article}
\usepackage{listings}
\lstdefinelanguage{iCli}
{
  comment=[l]{\%},
  basicstyle=\small,
  keywords={\#},
  keywordstyle=\textbf,
  delim=[s][keywordstyle]{(}{\#}
}

\begin{document}
\begin{lstlisting}[language=iCli]
% This is a comment
# configure terminal
(config)# json notification host a
\end{lstlisting}
\end{document}

But it does not highlight # as a keyword

  • Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – jub0bs Sep 26 '14 at 11:10
  • So, to be clear, you want to highlight (config) and (something-something) in red, but not the following # or the first #. Correct? What color should # characters be highlighted in? – jub0bs Sep 26 '14 at 11:13
  • I would like the text before the # to be red(or some other color), and then I would like all # to be bold, and the other text normal. – Allan Sep 26 '14 at 12:10
  • @Jubobs I added a compilable example – Allan Sep 26 '14 at 12:11
  • It would also be okay if I could get the complete prompt (from the beginning of the line to #) in bold. – Allan Sep 26 '14 at 12:13
1

A bit of hacking is required; see below.

enter image description here

\documentclass{article}

% the following two packages are for bold typewriter font
\usepackage[T1]{fontenc}
\usepackage{beramono}

\usepackage{listings}
\usepackage{xcolor}

\lstdefinelanguage{iCli}{
  comment    = [l]{\%},
  literate   = \#{{\processhash}}1,              %<-- required for bold prompt
  moredelim  = **[is][\processmydelims]{(}{)\#}, %<-- for highlighting (...)#
  basicstyle = \ttfamily,                        %<-- for a nice typewriter font
}

% helper macros
\newcommand\processmydelimsend{}
\newcommand\processmydelims{%
  \renewcommand\processmydelimsend{\textcolor{red}{)}\textbf{\#}\egroup}%
  \bgroup\color{red}(\aftergroup\processmydelimsend%
}
\makeatletter
\newcommand\processhash{%
  \ifnum\lst@mode=\lst@Pmode%
    \bfseries%
  \fi
  \#%
}
\makeatother

\begin{document}

\begin{lstlisting}[language=iCli]
# configure terminal
(config)# json notification host a
(something-something)# url http://10.99.10.1:8000/path
\end{lstlisting}

\end{document}
  • Thanks a lot for the response - it works but I have totally given up on understanding what it does, or why it is needed. I guess that this is what you mean with "A bit of hacking is required" :-) – Allan Sep 30 '14 at 13:53

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.