13

I am writing this report on a computing simulation of the Ising model. I am pressenting lattice configuration samples, which are just an array of "+" and "-". This is done in order to visualize the different "magnetic domains" which means I really want the "+" and "-" to stand out from each other. So I was wondering if there was an automated way for LaTeX to colour or, even better, highlight a certain character only within a verbatim. I am actually currently importing it from an external text file using VerbatimInput from the package fancyvrb So my code is:

\documentclass[a4paper,11pt]{article}
\usepackage{fancyvrb}
\usepackage{graphicx}
\usepackage{caption,subcaption}
\usepackage{relsize}

% redefine \VerbatimInput
\RecustomVerbatimCommand{\VerbatimInput}{VerbatimInput}%
{fontsize=\relsize{-3}
}

\begin{document}

    \begin{figure}[!ht]
    \centering
    \begin{subfigure}{0.5\textwidth}
      \centering
      \VerbatimInput{\detokenize{j0.2000_B0.0000_sweeps500_avaft100}}   
      \caption{$\frac{J}{kT}=0.2$}
    \end{subfigure}
    \end{figure}

\end{document}

I am compiling with XeLaTeX. The grid file is:

- - + - + - - - + - + - - - - - + + + + + - + + + - + + + -
+ - - + + - - - + + - + - + + + - - + + + + + + - + + - - +
- + - - - + - - + - + + + + + - + - + - - + - + - + + - + -
+ - + - - - + + + + - + - + + - + + + - + + - - + + - + + +
+ - + + + + + + + + - - + + - + + + + - + + + + + + + + + -
- - - + + + + - - + - - + - - - + + - - - - + - - - + + + -
- - - + - - - - - - + - + - - + + - - + + - + + + + + - - +
+ + - - - - - - + + + - - - + - - + - - - + + + - + - - + -
+ - - + + + - + + + - - + - - + - + - + + + - - - - - - - +
- + - - + - + + - + + - + - - - - - - + - - + - - + + + + +
- - - - - - - + + - - - + + - - + + - - - + - + - + - - - +
+ + + + + + - - + + + - + - - + - + - + + + + - - + + - + +
- + - + - + - + + + - + + - - - + + + + + - + - + - + - + -
- - + + + - - + + + + + - - - + + + - - - - - + - + + + - -
+ + + + - + + - - - + - - + + - - - + + - + + - + - + - - -
- + - + + + + + + - + - + + + + - - - + + + - + - + + - - +
+ + + + + - + + - - + + - - - + + + - + + - - + + + - - - -
+ + + + + - - + + + - - - - - + + + - + + + + + + - + - - +
- + - + + + + + - - - + + - + - + - - - + - - + + - - - - -
- - + - - + - + + + + + - - - - - - + + - + + + + - - + + +
+ - + + - - + + + - - + + - + + - - - + + - - + + + - + + +
+ - - + + - - - + - + - + + + + + - - - + + - - - - + + - -
- + + + - + + + + + - - + - + + + - + + - - + + + + + + + +
- + + + + + - + - + - - - - - + + + + + - - - - + - + + - +
- + + - - + + + - + - - + + - + + + - + - - + - + - + + + +
- - - - - - + - + + + + + + - - + + - + + + + + - - - + + -
- - + - - - + - - + - - - + - + - - + - - + + + - - + + + -
- - + - - - - - - + - - + + - + - + + - - + + - - + - - + +
- - - - + - - - - + - - - + - - - + - - - + + + - + + - - +
+ + - - - - - + + + - + - + - + - + - + - + + + + - + - - +
2
  • 1
    Are you amenable to using listings instead of fancyvrb?
    – jub0bs
    Commented May 12, 2014 at 14:31
  • Well, yes. Although facyvfb is preferable if possible, as it's more convenient to import the files.
    – guillefix
    Commented May 12, 2014 at 14:34

3 Answers 3

12

You can use defineactive to give the characters different attributes (see pages 20 and 21 of the fancyvrb documentation); an example using colors:

\documentclass[a4paper,11pt]{article}
\usepackage{fancyvrb}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{caption,subcaption}
\usepackage{relsize}

\usepackage{filecontents}
\begin{filecontents*}{j0.2000_B0.0000_sweeps500_avaft100}
- - + - + - - - + - + - - - - - + + + + + - + + + - + + + -
+ - - + + - - - + + - + - + + + - - + + + + + + - + + - - +
- + - - - + - - + - + + + + + - + - + - - + - + - + + - + -
+ - + - - - + + + + - + - + + - + + + - + + - - + + - + + +
+ - + + + + + + + + - - + + - + + + + - + + + + + + + + + -
- - - + + + + - - + - - + - - - + + - - - - + - - - + + + -
- - - + - - - - - - + - + - - + + - - + + - + + + + + - - +
+ + - - - - - - + + + - - - + - - + - - - + + + - + - - + -
+ - - + + + - + + + - - + - - + - + - + + + - - - - - - - +
- + - - + - + + - + + - + - - - - - - + - - + - - + + + + +
- - - - - - - + + - - - + + - - + + - - - + - + - + - - - +
+ + + + + + - - + + + - + - - + - + - + + + + - - + + - + +
- + - + - + - + + + - + + - - - + + + + + - + - + - + - + -
- - + + + - - + + + + + - - - + + + - - - - - + - + + + - -
+ + + + - + + - - - + - - + + - - - + + - + + - + - + - - -
- + - + + + + + + - + - + + + + - - - + + + - + - + + - - +
+ + + + + - + + - - + + - - - + + + - + + - - + + + - - - -
+ + + + + - - + + + - - - - - + + + - + + + + + + - + - - +
- + - + + + + + - - - + + - + - + - - - + - - + + - - - - -
- - + - - + - + + + + + - - - - - - + + - + + + + - - + + +
+ - + + - - + + + - - + + - + + - - - + + - - + + + - + + +
+ - - + + - - - + - + - + + + + + - - - + + - - - - + + - -
- + + + - + + + + + - - + - + + + - + + - - + + + + + + + +
- + + + + + - + - + - - - - - + + + + + - - - - + - + + - +
- + + - - + + + - + - - + + - + + + - + - - + - + - + + + +
- - - - - - + - + + + + + + - - + + - + + + + + - - - + + -
- - + - - - + - - + - - - + - + - - + - - + + + - - + + + -
- - + - - - - - - + - - + + - + - + + - - + + - - + - - + +
- - - - + - - - - + - - - + - - - + - - - + + + - + + - - +
+ + - - - - - + + + - + - + - + - + - + - + + + + - + - - +
\end{filecontents*}


% redefine \VerbatimInput
\RecustomVerbatimCommand{\VerbatimInput}{VerbatimInput}%
{fontsize=\relsize{-3}
}

\def\SumSign{\char43}
\def\MinusSign{\char45}

\begin{document}

\begin{figure}
  \centering
  \begin{subfigure}{0.5\textwidth}
  \centering
  \catcode`+=\active
  \catcode`-=\active
  \VerbatimInput[
    defineactive=%
    \def+{\color{blue}\SumSign}
    \def-{\color{red}\MinusSign}]{\detokenize{j0.2000_B0.0000_sweeps500_avaft100}}   
  \caption{$\frac{J}{kT}=0.2$}
  \end{subfigure}
\end{figure}

\end{document}

enter image description here

And another option:

\documentclass[a4paper,11pt]{article}
\usepackage{fancyvrb}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{caption,subcaption}
\usepackage{relsize}

\usepackage{filecontents}
\begin{filecontents*}{j0.2000_B0.0000_sweeps500_avaft100}
--+-+---+-+-----+++++-+++-+++-
+--++---++-+-+++--++++++-++--+
-+---+--+-+++++-+-+--+-+-++-+-
+-+---++++-+-++-+++-++--++-+++
+-++++++++--++-++++-+++++++++-
---++++--+--+---++----+---+++-
---+------+-+--++--++-+++++--+
++------+++---+--+---+++-+--+-
+--+++-+++--+--+-+-+++-------+
-+--+-++-++-+------+--+--+++++
-------++---++--++---+-+-+---+
++++++--+++-+--+-+-++++--++-++
-+-+-+-+++-++---+++++-+-+-+-+-
--+++--+++++---+++-----+-+++--
++++-++---+--++---++-++-+-+---
-+-++++++-+-++++---+++-+-++--+
+++++-++--++---+++-++--+++----
+++++--+++-----+++-++++++-+--+
-+-+++++---++-+-+---+--++-----
--+--+-+++++------++-++++--+++
+-++--+++--++-++---++--+++-+++
+--++---+-+-+++++---++----++--
-+++-+++++--+-+++-++--++++++++
-+++++-+-+-----+++++----+-++-+
-++--+++-+--++-+++-+--+-+-++++
------+-++++++--++-+++++---++-
--+---+--+---+-+--+--+++--+++-
--+------+--++-+-++--++--+--++
----+----+---+---+---+++-++--+
++-----+++-+-+-+-+-+-++++-+--+
\end{filecontents*}


% redefine \VerbatimInput
\RecustomVerbatimCommand{\VerbatimInput}{VerbatimInput}%
{fontsize=\relsize{-3}
}

\def\SumSign{\char43}
\def\MinusSign{\char45}

\begin{document}

\begin{figure}
  \centering
  \begin{subfigure}{0.75\textwidth}
  \centering
  \catcode`+=\active
  \catcode`-=\active
  \setlength\fboxsep{2pt}
  \offinterlineskip
  \VerbatimInput[
    defineactive=%
    \def+{\colorbox{red!70!black}{\textcolor{white}{\SumSign}}}
    \def-{\colorbox{cyan}{\textcolor{black}{\MinusSign}}}]{\detokenize{j0.2000_B0.0000_sweeps500_avaft100}}   
  \caption{$\frac{J}{kT}=0.2$}
  \end{subfigure}
\end{figure}

\end{document}

enter image description here

And with the help of egreg, here's a version using an environment:

\documentclass[a4paper,11pt]{article}
\usepackage{fancyvrb}
\usepackage{xcolor}
\usepackage{subcaption}
\usepackage{relsize}

% redefine \VerbatimInput
\RecustomVerbatimCommand{\VerbatimInput}{VerbatimInput}%
{fontsize=\relsize{-3}
}

\def\SumSign{\char43}
\def\MinusSign{\char45}

\makeatletter
\newenvironment{MyVerb}
  {
    \def\verbatim@nolig@list{\do\`\do\<\do\>\do\,\do\'}
    \catcode`+=\active
    \catcode`-=\active
    \setlength\fboxsep{2pt}
    \offinterlineskip
    \VerbatimEnvironment
    \begin{Verbatim}
  }
  {\end{Verbatim}}
\makeatother

\begin{document}

\begin{figure}[!ht]
  \centering
  \begin{subfigure}{0.75\textwidth}
  \begin{MyVerb}[defineactive=%
    \def+{\colorbox{red!70!black}{\textcolor{white}{\SumSign}}}
    \def-{\colorbox{cyan}{\textcolor{black}{\MinusSign}}}]
--+-+---+-+-----+++++-+++-+++-
+--++---++-+-+++--++++++-++--+
-+---+--+-+++++-+-+--+-+-++-+-
+-+---++++-+-++-+++-++--++-+++
+-++++++++--++-++++-+++++++++-
---++++--+--+---++----+---+++-
---+------+-+--++--++-+++++--+
++------+++---+--+---+++-+--+-
+--+++-+++--+--+-+-+++-------+
-+--+-++-++-+------+--+--+++++
-------++---++--++---+-+-+---+
++++++--+++-+--+-+-++++--++-++
-+-+-+-+++-++---+++++-+-+-+-+-
--+++--+++++---+++-----+-+++--
++++-++---+--++---++-++-+-+---
-+-++++++-+-++++---+++-+-++--+
+++++-++--++---+++-++--+++----
+++++--+++-----+++-++++++-+--+
-+-+++++---++-+-+---+--++-----
--+--+-+++++------++-++++--+++
+-++--+++--++-++---++--+++-+++
+--++---+-+-+++++---++----++--
-+++-+++++--+-+++-++--++++++++
-+++++-+-+-----+++++----+-++-+
-++--+++-+--++-+++-+--+-+-++++
------+-++++++--++-+++++---++-
--+---+--+---+-+--+--+++--+++-
--+------+--++-+-++--++--+--++
----+----+---+---+---+++-++--+
++-----+++-+-+-+-+-+-++++-+--+
  \end{MyVerb}   
  \caption{$\frac{J}{kT}=0.2$}
  \end{subfigure}
\end{figure}

\end{document}

enter image description here

6
  • 1
    For more automation, you should define a custom environment that changes the catcodes at the beginning.
    – jub0bs
    Commented May 12, 2014 at 14:53
  • How would I do that?
    – guillefix
    Commented May 12, 2014 at 15:06
  • 1
    @guillefix I'll add this in some minutes. Commented May 12, 2014 at 15:13
  • Wow, the answer is looking awesome. I see you deleted the spaces in between the + and -. Is there a way to automatize that as well?
    – guillefix
    Commented May 12, 2014 at 15:15
  • 1
    Looks like minesweeper.
    – jub0bs
    Commented May 12, 2014 at 15:17
6

No package needed, except for xcolor, subcaption and relsize that are irrelevant for the task at hand.

I give two examples, in the second one the definitions of \activeplus, \activeminus and \specialinputfont are changed, but the definition should probably be unique and placed in the preamble.

\documentclass[a4paper,11pt]{article}

\usepackage{xcolor}
\usepackage{caption,subcaption}
\usepackage{relsize}

\usepackage{filecontents}
\begin{filecontents*}{j0.2000_B0.0000_sweeps500_avaft100}
--+-+---+-+-----+++++-+++-+++-
+--++---++-+-+++--++++++-++--+
-+---+--+-+++++-+-+--+-+-++-+-
+-+---++++-+-++-+++-++--++-+++
+-++++++++--++-++++-+++++++++-
---++++--+--+---++----+---+++-
---+------+-+--++--++-+++++--+
++------+++---+--+---+++-+--+-
+--+++-+++--+--+-+-+++-------+
-+--+-++-++-+------+--+--+++++
-------++---++--++---+-+-+---+
++++++--+++-+--+-+-++++--++-++
-+-+-+-+++-++---+++++-+-+-+-+-
--+++--+++++---+++-----+-+++--
++++-++---+--++---++-++-+-+---
-+-++++++-+-++++---+++-+-++--+
+++++-++--++---+++-++--+++----
+++++--+++-----+++-++++++-+--+
-+-+++++---++-+-+---+--++-----
--+--+-+++++------++-++++--+++
+-++--+++--++-++---++--+++-+++
+--++---+-+-+++++---++----++--
-+++-+++++--+-+++-++--++++++++
-+++++-+-+-----+++++----+-++-+
-++--+++-+--++-+++-+--+-+-++++
------+-++++++--++-+++++---++-
--+---+--+---+-+--+--+++--+++-
--+------+--++-+-++--++--+--++
----+----+---+---+---+++-++--+
++-----+++-+-+-+-+-+-++++-+--+
\end{filecontents*}

\newcommand{\specialinput}[1]{%
  \begingroup
  \activateplus
  \activateminus
  \par\addvspace{\topsep}
  \specialinputfont\obeylines
  \input{#1}%
  \par\addvspace{\topsep}
  \endgroup
}

\newcommand{\activateplus}{%
  \begingroup\lccode`~=`+\lowercase{\endgroup\let~}\activeplus
  \catcode`+=\active
}
\newcommand{\activateminus}{%
  \begingroup\lccode`~=`-\lowercase{\endgroup\let~}\activeminus
  \catcode`-=\active
}

%%% change here the definitions
\newcommand{\specialinputfont}{\ttfamily\relsize{-3}}
\newcommand{\activeplus}{\textcolor{blue}{+}}
\newcommand{\activeminus}{\textcolor{red}{-}}


\begin{document}

\begin{figure}
\centering
\begin{subfigure}{0.48\textwidth}
\centering
\specialinput{j0.2000_B0.0000_sweeps500_avaft100}
\caption{$\frac{J}{kT}=0.2$}
\end{subfigure}\hfill
\begin{subfigure}{0.48\textwidth}
\centering
%%% change the definitions to make another example
\setlength{\fboxsep}{1pt}
\renewcommand{\activeplus}{\colorbox{red!70!black}{\textcolor{white}{+}}}
\renewcommand{\activeminus}{\colorbox{cyan}{\textcolor{black}{-}}}
\renewcommand{\specialinputfont}{\ttfamily\relsize{-3}\offinterlineskip}
\specialinput{j0.2000_B0.0000_sweeps500_avaft100}
\caption{$\frac{J}{kT}=0.2$}
\end{subfigure}%

\end{figure}

\end{document}

enter image description here

4

Here, I use active characters for plus and minus, and the verbatimbox package to set the items in a box, which can then be recalled inside a figure. The actively coded symbols can be turned on and off with \colorsymon and \colorsymoff.

\documentclass[a4paper,11pt]{article}
\usepackage{verbatimbox}
\usepackage{xcolor}

\let\svplus+
\let\svminus-
\catcode`+=\active
\catcode`-=\active
\def\colorsymon{%
  \catcode`+=\active\def+{\textcolor{red}{\ttfamily\svplus}}%
  \catcode`-=\active\def-{\textcolor{cyan!40}{\ttfamily\svminus}}%
}
\def\colorsymoff{%
  \catcode`+=12\let+\svplus%
  \catcode`-=12\let-\svminus%
}
\catcode`+=12
\catcode`-=12

\begin{document}

\colorsymon
\begin{verbbox}[\colorsymon]
- - + - + - - - + - + - - - - - + + + + + - + + + - + + + -
+ - - + + - - - + + - + - + + + - - + + + + + + - + + - - +
- + - - - + - - + - + + + + + - + - + - - + - + - + + - + -
+ - + - - - + + + + - + - + + - + + + - + + - - + + - + + +
+ - + + + + + + + + - - + + - + + + + - + + + + + + + + + -
- - - + + + + - - + - - + - - - + + - - - - + - - - + + + -
- - - + - - - - - - + - + - - + + - - + + - + + + + + - - +
+ + - - - - - - + + + - - - + - - + - - - + + + - + - - + -
+ - - + + + - + + + - - + - - + - + - + + + - - - - - - - +
- + - - + - + + - + + - + - - - - - - + - - + - - + + + + +
- - - - - - - + + - - - + + - - + + - - - + - + - + - - - +
+ + + + + + - - + + + - + - - + - + - + + + + - - + + - + +
- + - + - + - + + + - + + - - - + + + + + - + - + - + - + -
- - + + + - - + + + + + - - - + + + - - - - - + - + + + - -
+ + + + - + + - - - + - - + + - - - + + - + + - + - + - - -
- + - + + + + + + - + - + + + + - - - + + + - + - + + - - +
+ + + + + - + + - - + + - - - + + + - + + - - + + + - - - -
+ + + + + - - + + + - - - - - + + + - + + + + + + - + - - +
- + - + + + + + - - - + + - + - + - - - + - - + + - - - - -
- - + - - + - + + + + + - - - - - - + + - + + + + - - + + +
+ - + + - - + + + - - + + - + + - - - + + - - + + + - + + +
+ - - + + - - - + - + - + + + + + - - - + + - - - - + + - -
- + + + - + + + + + - - + - + + + - + + - - + + + + + + + +
- + + + + + - + - + - - - - - + + + + + - - - - + - + + - +
- + + - - + + + - + - - + + - + + + - + - - + - + - + + + +
- - - - - - + - + + + + + + - - + + - + + + + + - - - + + -
- - + - - - + - - + - - - + - + - - + - - + + + - - + + + -
- - + - - - - - - + - - + + - + - + + - - + + - - + - - + +
- - - - + - - - - + - - - + - - - + - - - + + + - + + - - +
+ + - - - - - + + + - + - + - + - + - + - + + + + - + - - +
\end{verbbox}
\colorsymoff

    \begin{figure}[!ht]
    \centering
      \centering
      \theverbbox
      \caption{$\frac{J}{kT}=0.2$}
    \end{figure}

    \begin{figure}[!ht]
    \centering
      \centering
      \colorsymon
      \verbfilebox[\colorsymon]{plusminus}
      \theverbbox
      \colorsymoff
      \caption{$\frac{J}{kT}=0.2$}
    \end{figure}

\end{document}

enter image description here

And the file input plusminus.tex:

enter image description here

And to get the full color effect... (EDITED TO REDUCE CODE SIZE)

\documentclass[a4paper,11pt]{article}
\usepackage{verbatimbox}
\usepackage{xcolor}

%ACTIVE PLUS AND MINUS SIGNS

\let\svplus+
\let\svminus-

\catcode`+=\active
\catcode`-=\active
\def\colorsymon{%
  \catcode`+=\active\def+{\usebox{\myplus}}%
  \catcode`-=\active\def-{\usebox{\myminus}}%
}
\def\colorsymoff{%
  \catcode`+=12\let+\svplus%
  \catcode`-=12\let-\svminus%
}
\catcode`+=12
\catcode`-=12

% DEFINE \myplus AND \myminus IN LITTLE BOXES
\def\mysize{\scriptsize}

\fboxrule=0pt\fboxsep=0pt\relax

\newsavebox\myplus\newsavebox\myminus
\savebox{\myplus}{\colorbox{cyan}{\mysize\makebox[\baselineskip]{\strut\textcolor{black}{\ttfamily+}}}}
\savebox{\myminus}{\colorbox{red}{\mysize\makebox[\baselineskip]{\strut\textcolor{white}{\ttfamily-}}}}

\begin{document}
\colorsymon
\begin{verbbox}[\mysize\colorsymon]
--+-+---+-+-----+++++-+++-+++-
+--++---++-+-+++--++++++-++--+
-+---+--+-+++++-+-+--+-+-++-+-
+-+---++++-+-++-+++-++--++-+++
+-++++++++--++-++++-+++++++++-
---++++--+--+---++----+---+++-
---+------+-+--++--++-+++++--+
++------+++---+--+---+++-+--+-
+--+++-+++--+--+-+-+++-------+
-+--+-++-++-+------+--+--+++++
-------++---++--++---+-+-+---+
++++++--+++-+--+-+-++++--++-++
-+-+-+-+++-++---+++++-+-+-+-+-
--+++--+++++---+++-----+-+++--
++++-++---+--++---++-++-+-+---
-+-++++++-+-++++---+++-+-++--+
+++++-++--++---+++-++--+++----
+++++--+++-----+++-++++++-+--+
-+-+++++---++-+-+---+--++-----
--+--+-+++++------++-++++--+++
+-++--+++--++-++---++--+++-+++
+--++---+-+-+++++---++----++--
-+++-+++++--+-+++-++--++++++++
-+++++-+-+-----+++++----+-++-+
-++--+++-+--++-+++-+--+-+-++++
------+-++++++--++-+++++---++-
--+---+--+---+-+--+--+++--+++-
--+------+--++-+-++--++--+--++
----+----+---+---+---+++-++--+
++-----+++-+-+-+-+-+-++++-+--+
\end{verbbox}
\colorsymoff

    \begin{figure}[!ht]
    \centering
      \centering
      \theverbbox
      \caption{$\frac{J}{kT}=0.2$}
    \end{figure}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .