1

I am using a single unified LaTeX doc to create problem sets and solutions:

   \item What is one plus one?
      \begin{soln}
         The answer is "two".
      \end{soln}

In LaTeX, I define this environment with (simplified):

\NewEnviron{soln}
  {
    \ifsolutions\expandafter
    \BODY
    \fi
  }

That is, if \solutionsfalse has been defined in LaTeX, it prints:

   1. What is one plus one?

and if \solutionstrue has been defined, it prints:

1. What is one plus one?
    ** The answer is two ** 

I'm trying to replicate this in pandoc to generate HTML or MD files from the latex input, but I've run against the wall. Pandoc doesn't honor any kind of /if /else /fi statement in LaTeX, I think. Pandoc doesn't honor the comment environment, which would also work with \excludecomment{soln}. So, I can't come up with a shim.tex file that would replicate the 'ignore stuff in the soln environment'.

The next way to go would, I guess, be to do something in luatex that pandoc can talk to, or to define the custom environment to pandoc with a filter? But the documentation for those systems is extremely heavyweight - there's no easy way in.

Can anyone suggest a solution to this?

Ideally, I want to run two different shell commands. Command A should omit all content in the soln environment. Command B, ideally, should turn all regular text blue, and show all content in the soln environment in black color.

(P.S. The xcolor package also seems unsupported in native pandoc, although there is a filter that doesn't work for me.)

Edit

Following comments by @tarleb and @mb21, I guess I have to try to work out how filters work. Again, the documentation here is terrible - it wants you to know everything before you can do anything.

I tried this:

return {
  {
    RawBlock = function(elem)
      print(elem.text)
      if starts_with('\\begin{soln}', elem.text) then        
        return pandoc.RawBlock(elem.format,"SOLN")
      else
        return elem
      end
    end,
  }
}

and ran it with

pandoc --lua-filter ifdef.lua --mathjax -s hw01.tex -s -o hw01.html

But there is nothing on stdout from the print statement, and my document is unchanged, so the RawBlocks are apparently not processed by the lua filter unless the -f latex+raw_tex flag is passed. But passing those flags means that pandoc doesn't actually process the \include commands in the latex, so my filter wont' see the subdocuments.

Apparently, the answer is "No, pandoc cannot support new latex environment", because it would require modifying the parser. Although the -f latex+raw_tex can disable big parts of the parser, that just means the document is largely unparsed, which isn't what I want.

Please tell me if I'm wrong.

Nathaniel Tagg
  • 385
  • 2
  • 14
  • if you want to do things like this, your live will be much easier if you use pandoc flavoured markdown as an input format, with it's native `div` elements etc. You can then manipulate the elements with [filters](https://pandoc.org/lua-filters.html). – mb21 Aug 18 '20 at 18:11
  • 1
    That's nice, but the input is already written, with an extensive list of macros and latex-specific formatting. The whole point is to convert existing source into another format, not start with another format. Why don't filters work on latex environments? – Nathaniel Tagg Aug 18 '20 at 22:33
  • filters absolutely do work when converting from other formats. have a look at `pandoc -t native` to see what you document looks like when it arrives in the filter stage.. – mb21 Aug 19 '20 at 08:04
  • 1
    Thanks, @mb21, but that's not enough to get going. That command shows that pandoc does not parse my `soln` environment at all, so it gets me no closer to a solution. – Nathaniel Tagg Aug 19 '20 at 13:21
  • Try with `pandoc -f latex+raw_tex` to force pandoc to pass all commands as raw TeX if it doesn't know how to parse them. – tarleb Aug 19 '20 at 19:50
  • If you have suggestions for documentation improvements, let us know. Calling it "terrible" is actually not appreciated. Don't forget that we built that stuff in our free time. – tarleb Aug 20 '20 at 20:09
  • Also: checkout `pandoc.read` – tarleb Aug 21 '20 at 08:07
  • Thanks tarleb, but I don't understand how that would get used in a filter? I guess you're suggesting having pandoc do a raw_tex read, then preprocessing my stuff, then running it through pandoc.read to do the final latex processing? That's a pretty hefty hack of the processing pipeline, no? – Nathaniel Tagg Aug 23 '20 at 15:30

0 Answers0