Skip to content
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

Reference and caption code chunk #238

Open
coatless opened this issue Nov 8, 2016 · 10 comments
Open

Reference and caption code chunk #238

coatless opened this issue Nov 8, 2016 · 10 comments
Labels
feature

Comments

@coatless
Copy link

@coatless coatless commented Nov 8, 2016

I'm interested in being able to reference code listings alongside a caption in a manner similar to figures. For example, if I had a code chunk called dobootstrap

It might look like so:

```{r dobootstrap, code.cap = "Illustrates the overall procedure for writing a simple random sample with replacement bootstrap."}
bootstrap = function(xt, B = 100, func = mean, ...) {
  results = rep(NA, B)   # Storage Vector
  n = length(xt)             # Length of Data
  
  for (i in seq_len(B)) {   # Begin Bootstrap
    xt_star  = xt[sample(n, n, replace = T)] # Generate new data
  results[i] = func(xt_star, ...) # Obtain statistic
  }
  
  results                         # Return bootstrapped test distribution
}
```

Then the reference would be \@ref(code:dobootstrap).

Presently, the only way to reference a chunk seems to be to have it generate a figure and apply a caption with fig.cap.

@yihui
Copy link
Member

@yihui yihui commented Nov 8, 2016

This is not possible at the moment. If I try hard enough, I can probably implement it, but I tend not to do it: https://bookdown.org/yihui/bookdown/faq.html

I guess I'm well-known to be opinionated, and here is my opinion on cross-references: I don't like them. As an author, I find it very convenient to refer readers to different parts of the document. As a reader, I find it more of a mess. Each cross-reference potentially means the reader has to flip some pages through the book. A lot of cross-references will make the book a tangled hairball. So I'll remind myself not to abuse cross-references when I write something lengthy. I admit I often fail, though. I succeeded in avoiding footnotes and parentheses as much as possible somehow, so readers don't have to move their eyes up and down, or be interrupted by a side note. Personally I find it helpful to explain things in a structure as linear as possible and not to connect all the dots explicitly. When I read a Sherlock Holmes story, I find I can connect the dots in my brain without the author telling me to go to Section 3.4 or see Figure 5.7, even if the story is quite complicated. I do admire authors of detective stories. It may not be fair to compare academic/technical writing with novels, but I'd ask myself time and time again if I could avoid redirecting readers when I write, in other words, can I make it clear in the first place so readers can just remember it?

Okay, I have digressed far enough :) Back to your request: I'm not saying a hard no. If it turns out the gain overweighs the pain, I'll certainly be happy to reconsider it (e.g. your request #152 was a good one, since theorems can make the logic more clear in math books).

@RichardJActon
Copy link

@RichardJActon RichardJActon commented May 10, 2018

I would very much like to see this feature implemented. I'm generally in agreement about excessive cross-referencing but in the case of code blocks I often find you want to be able to point to some code you have included in an appendix from a methods section, where you explain in board terms what the code does in the methods for non-coding types and then point to the specifics for those interested in the implementation.

@njtierney
Copy link

@njtierney njtierney commented Jun 13, 2018

I just wanted to add my voice here - it would be great to be able to reference code chunks in text, as I would like to draw the readers attention to the way that the code is written in a paper - so the feature is the code, rather than its output.

I have asked myself the question "Do I really need this?", and I think that the answer is yes - although I would also be very happy with an alternative, such as making the code into an algorithm format, or something similar.

I'm happy to try and work on this, if that would be helpful?

@yihui
Copy link
Member

@yihui yihui commented Jun 14, 2018

Okay, I think I have got enough votes for this feature. I still don't have time for it, but anyone please feel free to try. Note that you will have to make it work for at least PDF and HTML output. It will be great if it also works for EPUB and Word, but personally I don't care much about these formats.

@njtierney
Copy link

@njtierney njtierney commented Jun 14, 2018

OK great, I'm happy to give this a shot - I have been looking for an excuse to get into the internals of bookdown/rmarkdown, so let's see how I go - I don't think that I will get this feature done in time for the paper, so it might be a while - anyone reading this please feel free to have a go!

@ihrke
Copy link

@ihrke ihrke commented Sep 27, 2018

I would also be interested in this feature! Let me know if there is a way I can help!

@spegmb
Copy link

@spegmb spegmb commented Feb 24, 2020

Hi -- has this feature been implemented? Some publishers specifically require code chunks to be structured as captioned tables that can be referenced in the text, with proper sourcing information etc. I do think it would be very helpful to be able to use it.

@hongyuanjia
Copy link

@hongyuanjia hongyuanjia commented Mar 5, 2020

For those who interested, you may find this post on SO useful:

https://stackoverflow.com/questions/50702942/does-rmarkdown-allow-captions-and-references-for-code-chunks

Unfortunately, it only works for PDF output

@paulcbauer
Copy link

@paulcbauer paulcbauer commented May 12, 2020

I am also interested in this feature. I am currently working on a methods textbook. I totally agree with Yihui's points above (tangled hairball etc.), i.e., one should not reference things that are far away. However, atm I am using "see code below/above" (or something similar) to refer to code chunks and it would be much more exact to ref the chunk with an identifier.

@mattnolan001
Copy link

@mattnolan001 mattnolan001 commented Jul 17, 2020

It would be great to see this feature!

@cderv cderv added feature and removed enhancement labels Mar 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature
Projects
Status: Backlog
Development

No branches or pull requests

10 participants