HertieDataScience / SyllabusAndLectures

Hertie School of Governance Introduction to Collaborative Social Science Data Analysis
MIT License
37 stars 60 forks source link

question re image caption #48

Open ghost opened 8 years ago

ghost commented 8 years ago

i would like to include a caption below an external image in markdown. has anyone successfully done so and could share how you did it?

christophergandrud commented 8 years ago

Check out the answer to this StackExchange question.

Basically you need to add fig_caption: yes to your YAML header. For external images the caption is in the [ ], e.g.:

![Image Caption](plot_name.pdf)
mcallaghan commented 8 years ago

Hm. I still can't quite get it to work, even with fig_caption: true or fig_caption: yes.

Repo is here : https://github.com/mcallaghan/col_res_proj, the document is res_prop.Rmd.

I managed a workaround for html by writing this little bit of javascript at the bottom, but this is probably not ideal...

<script type="text/javascript">
var images = document.getElementsByTagName("img");
for (i = 0; i < images.length; i++) {
  var image = images[i];
  var n = i + 1
  var caption = 'figure ' + n + ': ' + image.alt;
  var figcaption = document.createElement('figcaption');
  var parent = image.parentNode;
  var figure = document.createElement('figure');
  parent.replaceChild(figure,image);
  figure.appendChild(image);
  figure.appendChild(figcaption);
  figcaption.innerHTML = caption;
}
</script>

Do we hand in our assignment in html or pdf format today?

christophergandrud commented 8 years ago

You almost have it. Notice that the figure caption does show up on the HTML:

image

(The default css for this is too understated for my liking, but that can be changed).

It is not showing up in your PDF probably because the fig_caption: true option is supplied to html_document, but not pdf_document in your YAML header. Add that in and it should work.

Also

    latex_engine: xelatex
    keep_tex: true

are TeX options so should be supplied to pdf_document, not html_document (for which they have no meaning).

christophergandrud commented 8 years ago

Also, I prefer PDF submissions for Assignment 2, but HTML is ok as long as it is formatted in an academic fashion.

mcallaghan commented 8 years ago

Thanks Christopher, I have one document and replace html_document with pdf_document when trying to produce a pdf, so have kept the pdf arguments in for when doing that.

But it doesn't create the captions even with pdf_document in the YAML header. They are showing in the html document because I created them with the javascript above...

jasmincantzler commented 8 years ago

I am trying the same thing and it also does not work for me, neither in the HTML nor the pdf.

On 23 October 2015 at 12:57, Max Callaghan notifications@github.com wrote:

Thanks Christopher, I have one document and replace html_document with pdf_document when trying to produce a pdf, so have kept the pdf arguments in for when doing that.

But it doesn't create the captions even with pdf_document in the YAML header. They are showing in the html document because I created them with the javascript above...

— Reply to this email directly or view it on GitHub https://github.com/HertieDataScience/SyllabusAndLectures/issues/48#issuecomment-150543008 .

christophergandrud commented 8 years ago

Hm that is weird. It appears to be something idiosyncratic to this document. For example, I just ran this document and it worked:

---
title: "Caption Test"
date: "23 October 2015"
output:
  pdf_document:
    fig_caption: yes
  html_document:
    fig_caption: yes
---

## R Markdown

Some Text Some TextSome Text Some Text Some Text Some Text Some Text Some Text 

![A Test](Greece-twitter-1.jpg)

I'll keep looking into it.

christophergandrud commented 8 years ago

I think I found the problem: it has to do with the image link link being on the "same line" as your commented code. Should be:

![picture \label{fig1}](img/Greece-twitter-1.jpg)

<!--we'd want to name the source of tables) -->

instead of:

![picture \label{fig1}](img/Greece-twitter-1.jpg)
<!--we'd want to name the source of tables) -->

Remember that in Markdown there are only line breaks when you have an empty line between text. To be honest, I'm not exactly sure why this is creates a problem with the captions. We would have to look behind the sense more in the Pandoc conversion between Markdown and HTML/TeX.

Just let me know if this doesn't solve the issue.

mcallaghan commented 8 years ago

That's it! So simple in the end, thanks so much

christophergandrud commented 8 years ago

Thanks of asking the question. Hopefully others will benefit from this discussion.

wwiebke commented 8 years ago

They already did ;)

bctclc commented 8 years ago

Do captions always appear below the figures? Any way to move them above the figures?

jasmincantzler commented 8 years ago

Another question I have is how to name the Figures; aka when I create a table in R, the caption appears below and simply states "Table1:" - how can I edit this to add a title?

Figured it out on my own. One need to add it here: