0

How can I insert the following WinBugs code into LaTeX as a text format?

  model
  {
    for(i in 1:nSUB)
    {
      s[i] ~ dnorm(0,tau.precision);
    }
  }

I tried \texttt{}:

\texttt{model
\{   \par
  for(i in 1:nSUB)   \par
  \{   \par
    s[i] $\sim$ dnorm(0,tau.precision);  \par
  \}  \par
\}
}

However, it gives a error message:

{model \{
! Paragraph ended before \text@command was complete. ....

I tried \usepackage{listings}, and used language R because there's no WinBugs language included.

\begin{lstlisting}[language=R]

model
{
  for(i in 1:nSUB)
  {
    s[i] ~ dnorm(0,tau.precision);
  }
}
\end{lstlisting}

There's no error message, but the text still looks very strange.

Stefan Pinnow
  • 29,535
celadonz
  • 101
  • verbatim environment is the simple approach. – Steven B. Segletes Apr 05 '18 at 17:37
  • 1
    HI, strange is a very relative concept, what do you mean by that ? – BambOo Apr 05 '18 at 18:09
  • @BambOo I assume that, by strange, the OP means non-proportional spacing. – Steven B. Segletes Apr 05 '18 at 18:51
  • @BambOo By strange I meant some words are bolded while some are not. I guess the bolded ones are considered as functions, but it's not consistent with WinBugs. Also, the ~ is weird looking. – celadonz Apr 05 '18 at 19:26
  • @celadonz, check the listings (here) or minted packages. Both of them have great documentation. Check this post for instance as a starting point. This will help you make your own language style. – BambOo Apr 05 '18 at 20:19
  • @StevenB.Segletes verbatim actually works quite well. My only complain is the ~ symbol looks strange. It's small and high. Do you know how to resolve this? – celadonz Apr 05 '18 at 20:32
  • While there are a number of alternatives people have tried to replace the tilde (see https://tex.stackexchange.com/questions/320855/why-is-tilde-not-available-out-of-the-box-in-tex/320883#320883), that technique does not work in verbatim. – Steven B. Segletes Apr 05 '18 at 21:05
  • However, this https://tex.stackexchange.com/questions/120797/tilde-in-verbatim may help, if you can adapt to listings. – Steven B. Segletes Apr 05 '18 at 21:11

1 Answers1

2

I don't know WinBugs, but turns out there's a lexer for pygments. You can use the minted environment as follows.

\documentclass{article}
\usepackage{minted}

\begin{document}
\begin{minted}{winbugs}
model
{
  for(i in 1:nSUB)
  {
    s[i] ~ dnorm(0,tau.precision);
  }
}
\end{minted}
\end{document}

The result looks quite reasonable although you can apply a different style if you prefer fewer or more colors. I tend to go with bw.

minted

tucuxi
  • 185
  • Your results look neat! However, I cannot reproduce on my computer. I pasted your original code to a tmp.tex file. When I compile it, there's an error message saying '!Latex Error: File 'tmp.pyg' not found'. Is there something I missed? – celadonz Apr 05 '18 at 20:21
  • Possibly you need to run with --shell-escape argument... Not sure because I haven't use it... Just check this: https://tex.stackexchange.com/a/99476/120578 – koleygr Apr 05 '18 at 20:26
  • Python and Pygments are prerequisites for minted, but easily installed as per section 2 of the minted manual. And you need to invoke latex with command-line option -shell-escape. – tucuxi Apr 05 '18 at 20:27
  • @tucuxi I see. Unfortunately this is a company machine and I don't have admin rights to install anything. Thanks though. – celadonz Apr 05 '18 at 20:31