-
-
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
Add support for alt text as short title in latex #2447
base: main
Are you sure you want to change the base?
Conversation
Nice catch @waeltken ! |
I think we need to update the documentation of the Images -> implicit_figures section in the readme. |
Let's see what @jgm first think about this feature. |
And maybe a test should ne nice too. |
Yep, i just saw that the test for the basic writer fails, so that's not good. |
diff --git a/tests/writer.latex b/tests/writer.latex
index 506c21d..0f29353 100644
--- a/tests/writer.latex
+++ b/tests/writer.latex
@@ -938,7 +938,7 @@ From ``Voyage dans la Lune'' by Georges Melies (1902):
\begin{figure}[htbp]
\centering
\includegraphics{lalune.jpg}
-\caption{lalune}
+\caption[Voyage dans la Lune]{lalune}
\end{figure}
Here is a movie \includegraphics{movie.jpg} icon. That should do the job for the test. |
Seems reasonable! Thanks! 😉 |
Well this actually works really nice for my thesis! So even if this get's rejected I am still very happy. 😄 |
+1 |
I have an issue with ![My long caption](figure.png "Short caption"){#fig:figure-label} produces \begin{figure}[htbp]
\centering
\includegraphics{figure.png}
\caption[]{\label{fig:figure-label}My long caption}
\end{figure} Instead of : \begin{figure}[htbp]
\centering
\includegraphics{figure.png}
\caption[Short caption]{\label{fig:figure-label}My long caption}
\end{figure} Looks like Any idea ? |
Seems to be a |
@waeltken Could you please apply this patch instead : diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs
index 0caa807..cbd6bd8 100644
--- a/src/Text/Pandoc/Writers/LaTeX.hs
+++ b/src/Text/Pandoc/Writers/LaTeX.hs
@@ -353,11 +353,14 @@ blockToLaTeX (Para [Image txt (src,'f':'i':'g':':':tit)]) = do
inNote <- gets stInNote
capt <- inlineListToLaTeX txt
img <- inlineToLaTeX (Image txt (src,tit))
+ short <- stringToLaTeX TextString tit
return $ if inNote
-- can't have figures in notes
then "\\begin{center}" $$ img $+$ capt $$ "\\end{center}"
else "\\begin{figure}[htbp]" $$ "\\centering" $$ img $$
- ("\\caption" <> braces capt) $$ "\\end{figure}"
+ if null short
+ then ("\\caption" <> braces capt) $$ "\\end{figure}"
+ else ("\\caption" <> brackets (text short) <> braces capt) $$ "\\end{figure}"
-- . . . indicates pause in beamer slides
blockToLaTeX (Para [Str ".",Space,Str ".",Space,Str "."]) = do
beamer <- writerBeamer `fmap` gets stOptions It removes the |
3fd9140
to
67f7c41
Compare
Thank you ! |
I think it’s okay now, right?
|
Yup |
A small issue I have. Text inside short caption are not rendered. For example |
You are right, that should definitely work. What's your resulting output? I assume that this line here:
does not produce the required markup. But I don't know how to parse that part of the title correctly at the moment. I guess our maintainer John might know. 😉 I'll look in to it later this week. You could try:
But right now i simply don't have the time and need for italics etc. in the short caption. |
I think the travis test fails because the patch is not applied to the master branch but the latest release version. I've done a rebase to the master branch, but i don't know how github handles this if i do a force push on that branch now. Will it keep the references to the related commits when the commit hash changes? |
I don't know why travis is failing. Rebase is often needed before merging to master. And yes the commit history will be kept. |
Use the images alt text as a short caption for the list of figures in latex documents. ![la lune](lalune.jpg "Voyage to the moon") Should now result in: \caption[Voyage to the moon]{la lune} in latex figures.
67f7c41
to
f05879d
Compare
+++ waeltken [Oct 13 15 05:16 ]:
That just does escaping that's needed for plain string Note that in Markdown images
the "alt" part is the alt text and the "title" part is the |
@hadim: So this would mean that we can not have markup e.g. italics in the title here then. @jgm: So do you see this as a useful addition? I mean the semantics to use the markdown title as a title for the list of figures in latex does not seem wrong to me. Of course the current version of pandoc does not support a list of figures, but people employing a specialized template might use it. |
f05879d
to
724f041
Compare
@hadim wrote:
There was an internal server error on the appveyor ci server. So i pushed the branch again to trigger ci again. It should pass now. |
I'm not sold on this. Why is it needed? Can you give a realistic example? |
Well since I get these emails, I'll chime in I guess. I used pandoc On Thu, Dec 3, 2015, 10:41 AM John MacFarlane notifications@github.com
.. typed on a tiny virtual keyboard |
Well I use it to have different caption below the figures and in the If you're against this feature, just tell me and I will maintain a patch for each new pandoc version. Thank you for your time anyway :-) |
OK, I see the point. My only reservation is that it might seem surprising to What about making the first sentence of the caption into the Another possibility would be to look for a span with class Example:
Thoughts? +++ Hadrien Mary [Dec 03 15 10:49 ]:
|
Pretty agnostic on specific syntax. Automatically using first sentence would be better than nothing, but a way Thanks for considering it further! On Thu, Dec 3, 2015, 12:19 PM Hadrien Mary notifications@github.com wrote:
.. typed on a tiny virtual keyboard |
Hey, so my feeling to this is that introducing the Although i liked the current version using the alt text the most, I've changed my workflow to use pandoc to create a tex file and then compile with xelatex using full blown latex figures. Anything else would not do for my thesis because i needed subfigures etc.. So I think the final decision should be made by the maintainer, since he has to live with it. 😉 |
Hi ! Hope you will be able to merge this soon :-) |
Ditto. This would be really handy. |
can this be accomplished a bit more explictly by using a "short-caption" or "short" attribute, which could be specially handled by the latex writer, much like "width" and "height" are specially handled attributes? example: ![This is a long caption](path/to/image.jpg){#myID short-caption="This is a short caption"} would get you \begin{figure}[htbp]
\centering
\includegraphics{path/to/image.jpg}
\caption[this is a short caption]{This is a long caption}\label{myID}
\end{figure} |
Hi, just wanted voice my support, and say that this feature would be pretty handy. |
I needed this, so I hacked together a patch to the current HEAD commit a088d67. This patch enables the attribute-style syntax discussed above. i.e.: ![This a really really long caption](path/to/image.jpg){#fig:ID lof-caption="Short capt for _LoF_"} Note that the lof-caption value will be interpreted from Markdown (using a fairly restrictive interpretation): I have had a think about how to apply this to Tables, but it's definitely beyond my Haskell capabilities for the moment. Notes:
|
Any news on this? Without markdown tables, figures, example lists and citations, the IMO most useful features of pandoc are unavailable for me, which is a real pitty. |
as a workaround, you can write a filter, see #3682 |
I definitely see the need for this, and many have chimed in that it would be useful. Putting the short caption in an attribute has the same drawback as using the title for this. Both are just plain strings, not parsed as formatted Markdown. This is too limiting, I think: you might, for example, want to have some math in a short caption. One possible solution is to create the short caption by concatening all the text in a span with class "short-caption". So, for example:
Maybe there are other possibilities along similar lines. If so, they should be suggested here. Or, if this is satisfactory, we could implement it fairly easily. |
The suggested solution would work for me. |
From an AST point of view, wrapping the image in a span is not too bad... from a markdown syntax point of view I find it quite ugly an error-prone. Are short-titles actually needed for all images, or only for figures? If the latter, maybe this should be fixed as part of the proposed native figure element. |
Consider this my vote for this feature in some form. My favorite options are:
Aside: If anybody has made or found a good filter for this purpose, please share! My thesis is due this month 😱 and my list of figures is 7 pages long. |
See improved filter below for a filter instead!!!
I cheated the system. All of my captions look something like this: ![SHORTMARK:This is my short caption. This is a really long caption that is almost certainly going to `cause` me problems I guess because $x^2 \leq x^3$?](path/to/figure) My build system involves a # create short captions based on first sentence, all captions of all figures
# have a single sentence ending with a *period*
def short_caption(match):
return "caption[{0}]{{{0}.".format(match.groups()[0])
# output is the string that is the output of `pandoc` on `stdout`
output = re.sub(r"caption{SHORTMARK:((?:[^.]+))[.]", short_caption, output) It's taking something of the form This was based off the first-sentence idea somewhere above. I forget where I saw it, but somewhere else somebody said to make a filter. So basically, if you really need it just create a unique marking scheme that you can perform a text replacement on later. Hope that is more helpful than confusing! |
Thanks, @svenevs I'll look into that!
That was likely this comment at #3682. Maybe a filter is more feasible for headers or other simpler objects than for images/figures? |
For the time being, here is a lua filter to do it. It also parses the title as markdown. -- don't do anything unless we target latex
if FORMAT ~= "latex" then
return {}
end
local latex_figure_start = [[
\begin{figure}
\centering
\includegraphics{%s}
]]
local latex_figure_end = '\\end{figure}'
function latex(str)
return pandoc.RawInline('latex', str)
end
function make_caption(long_caption, short_caption)
local caption = short_caption
table.insert(caption, 1, latex('\\caption['))
table.insert(caption, latex(']{'))
for i = 1, #long_caption do
caption[#caption + 1] = long_caption[i]
end
table.insert(caption, latex('}\n'))
return caption
end
function is_image_with_title(img)
return img.t == "Image" and img.title
end
function Para(para)
local img = para.content[1]
if not (#para.content == 1 and is_image_with_title(img)) then
return nil
end
local title = img.title:gsub('^fig:', '')
local title_inlines = pandoc.read(title).blocks[1].content
local figure = make_caption(img.caption, title_inlines)
local fig_start = latex(latex_figure_start:format(img.src))
local fig_end = latex(latex_figure_end)
table.insert(figure, 1, fig_start)
table.insert(figure, fig_end)
return pandoc.Plain(figure)
end Not the most beautiful, but should do what is asked for here. |
@tarleb very cool! Would you be willing to include a brief example of what you use in the markdown code? I would like to understand how this filter works, it seems I should really learn |
Lua filters were added to pandoc with release 2.0. Pandoc already had a Lua intepreter baked in (for custom writer), and we now enabled users to utilize it for filters as well. That's why it doesn't show up the "wrappers and interfaces" page. The code above could be saved to file An example would be the one given in the first comment here:
or, with some math in the short caption:
|
We should probably include this in @ickc's collection of lua filters. Here is a gist containing a slightly cleaner version. |
@tarleb your filter, does not work with pandoc-crossref, or does it? |
Sorry, I have overlooked your cleaner version from #2447 (comment) , which seems to work with pandoc-crossref. |
There's now a better and maintained version by @gtuckerkellogg in the pandoc/lua-filters repo. |
Use the images
alt texttitle as a short caption for the list of figures inlatex documents.
Should now result in:
in latex figures.