Pedro Fernandes
Pedro Fernandes

Reputation: 198

Problem with spec block in literate haskell file

I have this block of code on my literate haskell file

\end{code}


\paragraph{Valorização}

Codigo em C

\begin{spec}
double co(double x, int n){
  double a = 1;
  double b = -1 * x * x / 2;
  double c = 12;
  double d = 18;

  for(; n > 0; n--){
    a = a + b;
    b = b * (-1 * x * x) / c;
    c = c + d;
    d = 8 + d;
  }
  return a;
}
\end{spec}


\subsection*{Problema 4}

What's happening is, when using lhs2tex and the pdflatex, what's inside the spec block is being completely ignored, and everything after it is forward, like it has a tab before it... Maybe this is something common? I'm not used to this... First time using it

By the way, if I remove the spec block everything else is formatted correctly

Upvotes: 2

Views: 89

Answers (1)

HTNW
HTNW

Reputation: 29193

The following answer is based on speculation. If you would provide an MCVE—a short .lhs file that clearly demonstrates the issue—perhaps a better answer could emerge.

I think the issue is that lhs2TeX is not meant for C code. It gets confused by the spec block, thinks that it is Haskell code, and outputs problematic TeX commands. In fact, I can't even get your posted code past pdflatex—the .tex is that broken. You can use a different mechanism to output C code. The minted package should do.

\documentclass{article}
%include lhs2TeX.fmt
\usepackage{minted}

\setlength{\parindent}{0pt}

\begin{document}
Some C code:

\begin{minted}{c}
double co(double x, int n){
  double a = 1;
  double b = -1 * x * x / 2;
  double c = 12;
  double d = 18;

  for(; n > 0; n--){
    a = a + b;
    b = b * (-1 * x * x) / c;
    c = c + d;
    d = 8 + d;
  }
  return a;
}
\end{minted}

It can be directly translated into Haskell:

\begin{code}
co :: Double -> Int -> Double
co x = worker 1 (-1 * x * x / 2) 12 18
  where worker a _ _ _ 0 = a
        worker a b c d n = worker (a + b) (b * (-1 * x * x) / c) (c + d) (8 + d) (n - 1)
\end{code}

As you can see, \textit{Haskell} code passes through just fine.
\end{document}

PS: The weird for-loop can be written while(n-- > 0) { ... }, no?

Upvotes: 0

Related Questions