pandoc icon indicating copy to clipboard operation
pandoc copied to clipboard

Add support for alt text as short title in latex

Open waeltken opened this issue 10 years ago • 52 comments

Use the images ~~alt text~~ title 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.

waeltken avatar Oct 12 '15 11:10 waeltken

Nice catch @waeltken !

hadim avatar Oct 12 '15 11:10 hadim

I think we need to update the documentation of the Images -> implicit_figures section in the readme.

waeltken avatar Oct 12 '15 11:10 waeltken

Let's see what @jgm first think about this feature.

hadim avatar Oct 12 '15 11:10 hadim

And maybe a test should ne nice too.

hadim avatar Oct 12 '15 11:10 hadim

Yep, i just saw that the test for the basic writer fails, so that's not good.

waeltken avatar Oct 12 '15 11:10 waeltken

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.

hadim avatar Oct 12 '15 11:10 hadim

Seems reasonable! Thanks! :wink:

waeltken avatar Oct 12 '15 11:10 waeltken

Well this actually works really nice for my thesis! So even if this get's rejected I am still very happy. :smile:

waeltken avatar Oct 12 '15 11:10 waeltken

+1

hadim avatar Oct 12 '15 11:10 hadim

I have an issue with pandoc-crossref which I use for my thesis. This patch does not work with this plugin. See :

![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 text short is empty after the plugin pandoc-crossref.

Any idea ?

hadim avatar Oct 12 '15 11:10 hadim

Seems to be a pandoc-crossref related issue : https://github.com/lierdakil/pandoc-crossref/issues/38

hadim avatar Oct 12 '15 11:10 hadim

@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 [] if there is no alternative text in the image. Seems cleaner to me.

hadim avatar Oct 12 '15 12:10 hadim

Thank you !

hadim avatar Oct 12 '15 12:10 hadim

I think it’s okay now, right?

Am 12.10.2015 um 14:36 schrieb Hadrien Mary [email protected]:

Thank you, but I have updated the code since I did a mistake with $$ "\end{figure}"

— Reply to this email directly or view it on GitHub https://github.com/jgm/pandoc/pull/2447#issuecomment-147383831.

waeltken avatar Oct 12 '15 12:10 waeltken

Yup

hadim avatar Oct 12 '15 12:10 hadim

A small issue I have. Text inside short caption are not rendered. For example ![This is a long caption.](http://fakeimg.pl/439x320/282828/ "This a _short caption_ (alt text)") does not render short caption in italic.

hadim avatar Oct 12 '15 18:10 hadim

You are right, that should definitely work. What's your resulting output?

I assume that this line here:

short <- stringToLaTeX TextString tit

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. :wink: I'll look in to it later this week.

You could try:

short <- stringToLaTeX CodeString tit

But right now i simply don't have the time and need for italics etc. in the short caption.

waeltken avatar Oct 13 '15 12:10 waeltken

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?

waeltken avatar Oct 13 '15 12:10 waeltken

I don't know why travis is failing. Rebase is often needed before merging to master. And yes the commit history will be kept. Let's wait for @jgm to comment on the PR and handle travis stuff !

hadim avatar Oct 13 '15 12:10 hadim

+++ waeltken [Oct 13 15 05:16 ]:

I assume that this line here: short <- stringToLaTeX TextString tit

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. :wink: I'll look in to it later today or this week.

That just does escaping that's needed for plain string content in LaTeX (e.g. \% for %). It doesn't parse as Markdown.

Note that in Markdown images

![alt](url "title")

the "alt" part is the alt text and the "title" part is the title. The "alt" part does get parsed as Markdown, but the "title" part is just a plain string.

jgm avatar Oct 13 '15 17:10 jgm

Note that in Markdown images

![alt](url "title")

the "alt" part is the alt text and the "title" part is the title. The "alt" part does get parsed as Markdown, but the "title" part is just a plain string.

@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.

waeltken avatar Oct 14 '15 12:10 waeltken

@hadim wrote:

I don't know why travis is failing.

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.

waeltken avatar Oct 14 '15 12:10 waeltken

I guess we won't have markups in list of figures title... But I understand and I can live with that :-)

@jgm are you planning to merge this PR ?

hadim avatar Oct 14 '15 12:10 hadim

+++ Hadrien Mary [Oct 14 15 05:55 ]:

[1]@jgm are you planning to merge this PR ?

I don't know, I haven't had time to think it through yet.

jgm avatar Oct 14 '15 17:10 jgm

Just a small up so we don't forget this PR :-)

hadim avatar Dec 02 '15 23:12 hadim

I'm not sold on this. Why is it needed? Can you give a realistic example?

jgm avatar Dec 03 '15 18:12 jgm

Well since I get these emails, I'll chime in I guess. I used pandoc markdown for my phd thesis, and would have appreciated this feature. As it was, i had to go in by hand in the final version after converting it to latex & add alt captions to all the images so that the figure list up front with the TOC had reasonable one-line names in it, but figures in the body of the document could be captioned with a more descriptive two or three sentences.

On Thu, Dec 3, 2015, 10:41 AM John MacFarlane [email protected] wrote:

I'm not sold on this. Why is it needed? Can you give a realistic example?

— Reply to this email directly or view it on GitHub https://github.com/jgm/pandoc/pull/2447#issuecomment-161742789.

.. typed on a tiny virtual keyboard .. the usual requests for generosity in reading

el-ee avatar Dec 03 '15 18:12 el-ee

Well I use it to have different caption below the figures and in the \listoffigures section. For example for my phd thesis I use a lot of figures with very long caption and I don't want to display all the captions in \listoffigures. This is why I use short title mechanism to be able to display only a short sentence summarizing the figure.

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 :-)

hadim avatar Dec 03 '15 18:12 hadim

OK, I see the point.

My only reservation is that it might seem surprising to overload the title in this way. Lots of people may use a blank title, or they may want the title to have something else, and it may be unexpected/break existing workflows to have it become the short title.

What about making the first sentence of the caption into the short title? Would that work or would it still be too long?

Another possibility would be to look for a span with class "short-caption" inside the caption; if found, it could be used as the short caption.

Example:

![<span class="short-caption">This is my figure.</span>
It has a much longer caption, but only that first
bit goes into the short caption.](url)

Thoughts?

+++ Hadrien Mary [Dec 03 15 10:49 ]:

Well I use it to have different caption below the figures and in the \listoffigures section. For example for my phd thesis I use a lot of figures with very long caption and I don't want to display all the captions in \listoffigures. This is why I use short title mechanism to be able to display only a short sentence summarizing the figure.

If you're against this feature, just tell it and will maintain a patch for each new pandoc version.

Thank you for your time anyway :-)

— Reply to this email directly or [1]view it on GitHub.

References

  1. https://github.com/jgm/pandoc/pull/2447#issuecomment-161744773

jgm avatar Dec 03 '15 19:12 jgm

Well I don't have any specific preferences about this. I am ok as I can specify a short caption :-)

Maybe we should ask to @el-ee and @waeltken .

hadim avatar Dec 03 '15 19:12 hadim