-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Option for wrapping block codes with minipages (LaTeX) #703
Comments
I don't think it would be good to add it, because some people +++ Aviram Segal [Jan 16 13 06:20 ]:
|
I mean add an option for it that is turned off by default |
Have you considered a pure LaTeX solution? Use a custom latex template, in which you redefine the lstlisting environment, which is what pandoc emits when the --listings option is used. |
I spent 2 whole days trying to do that with no success |
You might ask on the pandoc-discuss list. There are some people +++ Aviram Segal [Jan 16 13 08:42 ]:
|
After I got no reply for 2 days I consulted with somebody who uses latex a lot |
Added a pull request (#707) for this issue, I guess we can close this one |
I've also tried this repeatedly using LaTeX alone plus asked on the TeX list and stackexchange without success. I spent about two weeks hammering on the problem. As far as I am concerned @aviramsegal's solution is ideal. +1. |
Another possibility would be to make the minipage sensitive to the
would wrap the thing in a minipage (whether or not listings is used). +++ James Turnbull [Jan 20 13 08:09 ]:
|
Personally I have a problem with that because it will require me to pre-process the markdown file as they already have a ````` code block styles and I can not edit them. |
It occurs to me that you could easily add the minipage instructions Write a function Block -> [Block] that matches code blocks and As your last comment shows, people's needs in this area are going to |
That's well beyond my limited Haskell skills I am afraid but hopefully someone else can chime in with a script that does it. |
This should do it import Text.Pandoc
main = toJsonFilter addMinipages
addMinipages :: Block -> [Block]
addMinipages (CodeBlock attr code) =
[ RawBlock "latex" "\\begin{minipage}{\\linewidth}"
, CodeBlock attr code
, RawBlock "latex" "\\end{minipage}" ]
addMinipages b = [b] Save this as minipage.hs, compile with 'ghc --make minipage', pandoc -t json | ./minipage | pandoc -f json -t latex +++ James Turnbull [Jan 20 13 16:05 ]:
|
John Works perfectly! Thank you very much. |
Okay small issue. Testing worked perfectly but on my whole book I get: minipage: Unable to read String Any clues on where I should start debugging? |
+++ James Turnbull [Jan 20 13 17:01 ]:
That's a strange one. Pandoc uses the json package's generic One way to debug would be to create a file with pandoc's
Then feed that file to minipage:
Presumably you'll get the error here. But now you can If it's not invalid, then it's possible that the fault lies |
Upgrading to 1.10 removed the error but I am noticing it's generating multiple minipages: \begin{minipage}{\linewidth} \begin{minipage}{\linewidth} \begin{minipage}{\linewidth} \begin{minipage}{\linewidth} \begin{minipage}{\linewidth} \begin{minipage}{\linewidth} \begin{minipage}{\linewidth} \begin{minipage}{\linewidth} \begin{minipage}{\linewidth} the listing... \end{minipage} \end{minipage} \end{minipage} \end{minipage} \end{minipage} \end{minipage} \end{minipage} \end{minipage} \end{minipage} \end{minipage} \end{minipage} \end{minipage} \end{minipage} \end{minipage} \end{minipage} \end{minipage} |
Ah yes. I see what is happening. Note that a Haskell list contains One workaround is to use this:
It will only operate on top-level block elements, so it won't work if This is a limitation of the scripting API; it should be easier to +++ James Turnbull [Jan 20 13 17:51 ]:
|
Awesome! Fixed. You've gone above and beyond on this and I really appreciate it. |
Thank you so much James! Obviously I came across the issue you described with code blocks in a list, I made a fix and I would love to get your feedback on it. Basically what I did is that if I see an import Text.Pandoc
main = interact $ jsonFilter $ \(Pandoc meta blocks) ->
Pandoc meta (addMinipages blocks)
addMinipages :: [Block] -> [Block]
addMinipages (OrderedList attr lst : xs) =
[ OrderedList attr (map addMinipages lst) ] ++ addMinipages xs
addMinipages (BulletList lst : xs) =
[ BulletList (map addMinipages lst) ] ++ addMinipages xs
addMinipages (CodeBlock attr code : xs) =
[ RawBlock "latex" "\\begin{minipage}{\\linewidth}"
, CodeBlock attr code
, RawBlock "latex" "\\end{minipage}" ]
++ addMinipages xs
addMinipages (x:xs) = x : addMinipages xs
addMinipages [] = [] |
Here's a variant that should work on all embeddings import Text.Pandoc
main = toJsonFilter addMinipages
addMinipages :: [Block] -> [Block]
addMinipages (CodeBlock attr code : xs)
| not (beginsWithEndMinipage xs) =
[ RawBlock "latex" "\\begin{minipage}{\\linewidth}"
, CodeBlock attr code
, RawBlock "latex" "\\end{minipage}" ]
++ addMinipages xs
addMinipages (x:xs) = x : addMinipages xs
addMinipages [] = []
beginsWithEndMinipage (RawBlock "latex" "\\end{minipage}":_) = True
beginsWithEndMinipage _ = False |
Is there any way to make smaller code blocks not wrap but allow longer ones to span multiple pages? I have a document with a mix of short in-line examples which look really bad wrapped over pages and longer code listings which are too big to fit on a single page. |
I haven't been being careful about scripts not spanning multiple pages, but I have at times just put in a human readable link in a footnote for where to see the code. Such as this: Code listing^code can be found in the footnotes. Also, it seems that if the link is https: it breaks the display in the footer. So I have to make those un-clickable for PDF. |
I just upgraded to 1.12.3 and I now get: minipages: fromJSON: field does not exist docTitle When I run the script above. I've had a bit of poke but I can't see what's broken. I tried to compile the later script in #703 (comment) but got: [1 of 1] Compiling Main ( minipages.hs, minipages.o )
minipages.hs:3:8:
No instance for (ToJsonFilter ([Block] -> [Block]))
arising from a use of `toJsonFilter'
Possible fix:
add an instance declaration for (ToJsonFilter ([Block] -> [Block]))
In the expression: toJsonFilter addMinipages
In an equation for `main': main = toJsonFilter addMinipages
minipages.hs:8:24:
Couldn't match expected type `Format' with actual type `[Char]'
In the first argument of `RawBlock', namely `"latex"'
In the expression:
RawBlock "latex" "\\begin{minipage}{\\linewidth}"
In the first argument of `(++)', namely
`[RawBlock "latex" "\\begin{minipage}{\\linewidth}",
CodeBlock attr code, RawBlock "latex" "\\end{minipage}"]'
minipages.hs:15:33:
Couldn't match expected type `Format' with actual type `[Char]'
In the pattern: "latex"
In the pattern: RawBlock "latex" "\\end{minipage}"
In the pattern: RawBlock "latex" "\\end{minipage}" : _ Any ideas? |
Modified for latest API (not tested though): import Text.Pandoc
main = toJsonFilter addMinipages'
addMinipages' :: Pandoc -> Pandoc
addMinipages' = bottomUp addMinipages
addMinipages :: [Block] -> [Block]
addMinipages (CodeBlock attr code : xs)
| not (beginsWithEndMinipage xs) =
[ RawBlock (Format "latex") "\\begin{minipage}{\\linewidth}"
, CodeBlock attr code
, RawBlock (Format "latex") "\\end{minipage}" ]
++ addMinipages xs
addMinipages (x:xs) = x : addMinipages xs
addMinipages [] = []
beginsWithEndMinipage (RawBlock (Format "latex") "\\end{minipage}":_) = True
beginsWithEndMinipage _ = False |
I got this to work with the following modified version. import Text.Pandoc
import Text.Pandoc.JSON
main = toJSONFilter addMinipages'
addMinipages' :: Pandoc -> Pandoc
addMinipages' = bottomUp addMinipages
addMinipages :: [Block] -> [Block]
addMinipages (CodeBlock attr code : xs)
| not (beginsWithEndMinipage xs) =
[ RawBlock (Format "latex") "\\begin{minipage}{\\linewidth}"
, CodeBlock attr code
, RawBlock (Format "latex") "\\end{minipage}" ]
++ addMinipages xs
addMinipages (x:xs) = x : addMinipages xs
addMinipages [] = []
beginsWithEndMinipage (RawBlock (Format "latex") "\\end{minipage}":_) = True
beginsWithEndMinipage _ = False |
Can anyone explain how can I use that code with pandoc to get the desired result? |
Curretnly block codes are able to split between pages. for preventing it it is possible to wrap it with a minipage:
\begin{minipage}{linewidth}
\begin{listings}
some code
\end{listings}
\end{minipage}
I downloaded the sources and added it manually for now, I tried doing something more general to suggest as a pull request but Haskell kicked me to the curve.
I hope you will be kind enough to add it, it should be very simple
The text was updated successfully, but these errors were encountered: