LaTeX forum ⇒ Text Formattinglistings | Syntax Highlighting for VDHL Topic is solved

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Shellduck
Posts: 2
Joined: Mon Dec 03, 2012 12:42 pm

listings | Syntax Highlighting for VDHL

Postby Shellduck » Mon Dec 03, 2012 1:06 pm

Hi

My professor won't approve a hand in due to no highlighting in the code (VHDL programing).

I have now spend then entire morning trying to find a package that can do this highlighting, but without results. according to the documentation for the listings package, it should be able to do it, but I have not been able to do it (also found other sources that says that the lstlisting environment doesn't recognize VHDL code.)

Is it hidden very well, not existing or am I just to stupid to find it?

If it is non existing, is there some guide to set up a custom languid with the listings package?

Recommended reading 2021:

LaTeXguide.org • LaTeX-Cookbook.net
LaTeX Beginner's Guide LaTeX Cookbook
User avatar
cgnieder
Site Moderator
Posts: 2001
Joined: Sat Apr 16, 2011 7:27 pm

listings | Syntax Highlighting for VDHL  Topic is solved

Postby cgnieder » Mon Dec 03, 2012 2:11 pm

Hi Shellduck and welcome to the LaTeX community!

I do not know of any guide but the listings documentation is very comprehensive. It is not too hard to get started.

I have no knowledge whatsoever of VHDL so the following is tailored following the Wikipedia example. Maybe it can be a starting point:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{beramono}% monospaced font with bold variant

\usepackage{listings}
\lstdefinelanguage{VHDL}{
  morekeywords={
    library,use,all,entity,is,port,in,out,end,architecture,of,
    begin,and
  },
  morecomment=[l]--
}

\usepackage{xcolor}
\colorlet{keyword}{blue!100!black!80}
\colorlet{comment}{green!90!black!90}
\lstdefinestyle{vhdl}{
  language     = VHDL,
  basicstyle   = \ttfamily,
  keywordstyle = \color{keyword}\bfseries,
  commentstyle = \color{comment}
}

\begin{document}

\begin{lstlisting}[style=vhdl]
-- (this is a VHDL comment)
 
-- import std_logic from the IEEE library
library IEEE;
use IEEE.std_logic_1164.all;
 
-- this is the entity
entity ANDGATE is
  port ( 
    I1 : in std_logic;
    I2 : in std_logic;
    O  : out std_logic);
end entity ANDGATE;
 
-- this is the architecture
architecture RTL of ANDGATE is
begin
  O <= I1 and I2;
end architecture RTL;
\end{lstlisting}

\end{document}


vhdl.png
vhdl.png (35.11 KiB) Viewed 16000 times


Regards
site moderator & package author

Shellduck
Posts: 2
Joined: Mon Dec 03, 2012 12:42 pm

listings | Syntax Highlighting for VDHL

Postby Shellduck » Mon Dec 03, 2012 2:56 pm

hi cgnieder and thanks for the welcome :)

This is perfect, and just what I was looking for!:)

Just one question, if I want to make two kinds of keywords, I write:
...
\lstdefinelanguage{VHDL}{
  morekeywords=[a]{
    library,use,all,entity,is,port,in,out,end,architecture,of,
    begin,and
  },
  morekeywords=[b]{
    STD_LOGIC_VECTOR,STD_LOGIC
  },

  morecomment=[l]--
}
\usepackage{xcolor}
\colorlet{keyword1}{blue!100!black!80}
\colorlet{keyword2}{blue!50!black!80}
\colorlet{comment}{green!90!black!90}
\lstdefinestyle{vhdl}{
  language     = VHDL,
  basicstyle   = \ttfamily,
  keywordstyle[a] = \color{keyword1}\bfseries,
  keywordstyle[b] = \color{keyword2}\bfseries,
  commentstyle = \color{comment}
}

right? (dunno if the color code I used is real, but...

EDIT: after a little work around it worked:)
\lstdefinelanguage{VHDL}{
  morekeywords=[1]{
    library,use,all,entity,is,port,in,out,end,architecture,of,
    begin,and,or,Not,downto,ALL
  },
  morekeywords=[2]{
    STD_LOGIC_VECTOR,STD_LOGIC,IEEE,STD_LOGIC_1164,
    NUMERIC_STD,STD_LOGIC_ARITH,STD_LOGIC_UNSIGNED,std_logic_vector,
    std_logic
  },
  morecomment=[l]--
}
\usepackage[usenames,dvipsnames]{xcolor}
\colorlet{keyword}{blue!100!black!80}
\colorlet{STD}{Lavender}
\colorlet{comment}{green!80!black!90}
\lstdefinestyle{vhdl}{
  language     = VHDL,
  basicstyle   = \footnotesize \ttfamily,
  keywordstyle = [1]\color{keyword}\bfseries,
  keywordstyle = [2]\color{STD}\bfseries,
  commentstyle = \color{comment}
  breaklines=true,                % sets automatic line breaking
  tabsize=3		                   % sets default tabsize to 2 spaces
}

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

listings | Syntax Highlighting for VDHL

Postby localghost » Mon Dec 03, 2012 8:10 pm

The minted package, which uses the Python syntax highlighter Pygments, has native support for VHDL.
\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{minted}
\usepackage{bera}

\begin{document}
  \begin{minted}[gobble=4]{vhdl}
    -- (this is a VHDL comment)

    -- import std_logic from the IEEE library
    library IEEE;
    use IEEE.std_logic_1164.all;

    -- this is the entity
    entity ANDGATE is
      port (
        I1 : in std_logic;
        I2 : in std_logic;
        O  : out std_logic);
    end entity ANDGATE;

    -- this is the architecture
    architecture RTL of ANDGATE is
    begin
      O <= I1 and I2;
    end architecture RTL;
  \end{minted}
\end{document}

For details please refer to the package manual. The package needs Python and Pygments installed. The compiler has to be run with shell escape.
Attachments
wtmp.png
The resulting output of the provided code example.
wtmp.png (10.04 KiB) Viewed 15984 times
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

shrican
Posts: 1
Joined: Tue Jul 19, 2022 10:48 am

listings | Syntax Highlighting for VDHL

Postby shrican » Tue Jul 19, 2022 10:49 am

Shellduck wrote:hi cgnieder and thanks for the welcome :)

This is perfect, and just what I was looking for!:)

Just one question, if I want to make two kinds of keywords, I write:
...
\lstdefinelanguage{VHDL}{
  morekeywords=[a]{
    library,use,all,entity,is,port,in,out,end,architecture,of,
    begin,and
  },
  morekeywords=[b]{
    STD_LOGIC_VECTOR,STD_LOGIC
  },

  morecomment=[l]--
}
\usepackage{xcolor}
\colorlet{keyword1}{blue!100!black!80}
\colorlet{keyword2}{blue!50!black!80}
\colorlet{comment}{green!90!black!90}
\lstdefinestyle{vhdl}{
  language     = VHDL,
  basicstyle   = \ttfamily,
  keywordstyle[a] = \color{keyword1}\bfseries,
  keywordstyle[b] = \color{keyword2}\bfseries,
  commentstyle = \color{comment}
}

right? (dunno if the color code I used is real, but...

EDIT: after a little work around it worked:)
\lstdefinelanguage{VHDL}{
  morekeywords=[1]{
    library,use,all,entity,is,port,in,out,end,architecture,of,
    begin,and,or,Not,downto,ALL
  },
  morekeywords=[2]{
    STD_LOGIC_VECTOR,STD_LOGIC,IEEE,STD_LOGIC_1164,
    NUMERIC_STD,STD_LOGIC_ARITH,STD_LOGIC_UNSIGNED,std_logic_vector,
    std_logic
  },
  morecomment=[l]--
}
\usepackage[usenames,dvipsnames]{xcolor}
\colorlet{keyword}{blue!100!black!80}
\colorlet{STD}{Lavender}
\colorlet{comment}{green!80!black!90}
\lstdefinestyle{vhdl}{
  language     = VHDL,
  basicstyle   = \footnotesize \ttfamily,
  keywordstyle = [1]\color{keyword}\bfseries,
  keywordstyle = [2]\color{STD}\bfseries,
  commentstyle = \color{comment}
  breaklines=true,                % sets automatic line breaking
  tabsize=3		                   % sets default tabsize to 2 spaces
}


This is great! I'm going to shamelessly copy this into my project! Thank you, Shellduck!


Return to “Text Formatting”

Who is online

Users browsing this forum: No registered users and 2 guests