Text Formattinglistings | Syntax Highlighting for VDHL

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

listings | Syntax Highlighting for VDHL

Post by Shellduck »

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 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

listings | Syntax Highlighting for VDHL

Post by cgnieder »

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:

Code: Select all

\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 21708 times
Regards
site moderator & package author
Shellduck
Posts: 2
Joined: Mon Dec 03, 2012 12:42 pm

listings | Syntax Highlighting for VDHL

Post by Shellduck »

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:

Code: Select all

...
\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:)

Code: Select all

\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

Post by localghost »

The minted package, which uses the Python syntax highlighter Pygments, has native support for VHDL.

Code: Select all

\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
The resulting output of the provided code example.
The resulting output of the provided code example.
wtmp.png (10.04 KiB) Viewed 21692 times
shrican
Posts: 1
Joined: Tue Jul 19, 2022 10:48 am

listings | Syntax Highlighting for VDHL

Post by shrican »

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:

Code: Select all

...
\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:)

Code: Select all

\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!
Post Reply