rstudio / tufte

Tufte Styles for R Markdown Documents
https://rstudio.github.io/tufte/
401 stars 110 forks source link

{marginfigure} and main text alignment issue #62

Open eijoac opened 6 years ago

eijoac commented 6 years ago

The Tufte pdf handout rendering seems to have an issue aligning 'margin figure' and main text. I believe the top of the 'margin figure' should align with the first line of the text after the {marginfigure} markdown block, but it doesn't. The html rendering is good. (i am not sure whether this is a pandoc problem or tufte package problem though.)

pdf (not correct)

image

html (good)

image

singmann commented 4 years ago

For the pdf version you can use the fig.pos chunk option to use figures (not sure about other margin elements). For example, fig.pos="4cm" moves a margin figure 4 cm down (negative values move it up).

danieleghisi commented 4 years ago

Hi, I'm having the same issue. It looks like it is vertically center-aligned rather than top-aligned to the next line. Since I was planning to use this a lot, I was wondering whether anyone had some sort of general workaround (each of my many images will have a different size, and retouching the position of all of them looks like a nightmare to maintain... even just changing a caption destroys the alignment...)

zenzike commented 4 years ago

For me this issue is fixed with the code in the accepted answer here: https://tex.stackexchange.com/questions/193137/modify-the-tuftemarginfloat-environment-to-automatically-align-floats

r-3141592-pi commented 2 months ago

I'm experiencing this same issue. Unfortunately, the suggested workarounds are not fixing the problem. For example, using the fig.pos chunk option requires manual adjustment for each figure and is not maintainable for documents with many figures. On the other hand, the LaTeX solution suggested in (https://github.com/rstudio/tufte/issues/62#issuecomment-725731888) relies on hardcoded values and doesn't behave well with subsequent figures. It also doesn't align correctly in many cases.

As the package heavily relies on these margin figures, any code blocks exceeding a few lines, such as those commonly used with ggplot, make the alignment issues more noticeable.

This is my minimal reproducible example:

 ---

title: "An Example Using the Tufte Style"
output: 
    tufte::tufte_handout:
        latex_engine: xelatex
--- 

# Load required libraries
'''{r setup}
library(dplyr)
library(ggplot2)
library(tufte)
'''

## Test

'''{r, fig.margin = TRUE}
# Load the mtcars dataset
data(mtcars)
# Create a scatter plot
ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
  geom_point(size = 3, alpha = 0.7) +
  geom_smooth(method = "lm", se = FALSE) +
  labs(title = "Car Weight vs. MPG by Number of Cylinders",
       x = "Weight (1000 lbs)",
       y = "Miles per Gallon",
       color = "Cylinders") +
  theme_minimal() +
  scale_color_brewer(palette = "Set1")
'''

screenshot-240910-1541-01