Skip to content

Commit

Permalink
Figures in jou mode fit to column width automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
wjschne committed Mar 5, 2024
1 parent 5d78c3e commit 09bd901
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 21 deletions.
6 changes: 4 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
# Future Version Wishlist

- Create landscape pages for wide figures and tables.
- Figure and Tables in .pdf jou mode should fit automatically.
- Tables in .pdf jou mode should fit automatically.
- Typst version that would allow for easy customization

# Version 3.3.1 (2024-03-04)
# Version 3.4.0 (2024-03-04)

- Implemented [language
options](https://wjschne.github.io/apaquarto/options.html#language-options)
for internationalization and customization.
- Figures in `jou` mode fit to column width automatically. Can be
overridden with `fig-width` or `out-width`.

# Version 3.3.0 (2024-02-29)

Expand Down
3 changes: 2 additions & 1 deletion NEWS.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ engine: knitr
# Future Version Wishlist

- Create landscape pages for wide figures and tables.
- Figure and Tables in .pdf jou mode should fit automatically.
- Tables in .pdf jou mode should fit automatically.
- Typst version that would allow for easy customization

# Version 3.4.0 (2024-03-04)

- Implemented [language options](https://wjschne.github.io/apaquarto/options.html#language-options) for internationalization and customization.
- Figures in `jou` mode fit to column width automatically. Can be overridden with `fig-width` or `out-width`.

# Version 3.3.0 (2024-02-29)

Expand Down
5 changes: 2 additions & 3 deletions _extensions/wjschne/apaquarto/apafigurelatex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ if FORMAT ~= "latex" then
return
end


Div = function(div)

if div.attributes then
if div.attributes then

if div.attributes["apa-note"] then
if div.identifier:find("^fig%-") then
Expand Down
33 changes: 33 additions & 0 deletions _extensions/wjschne/apaquarto/apafigurewidthlatex.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
if FORMAT ~= "latex" then
return
end

Div = function(div)
if div.identifier:find("^fig%-") then
div.content = div.content:walk {
Image = function(img)
if img.attributes.width == nil then
img.attributes.width = "\\columnwidth"
end
return img
end
}
end

div.content = div.content:walk {
Figure = function(fg)
if fg.identifier:find("^fig%-") then
fg.content = fg.content:walk {
Image = function(img)
if img.attributes.width == nil then
img.attributes.width = "\\columnwidth"
end
return img
end
}
return fg
end
end
}
return div
end
24 changes: 9 additions & 15 deletions template.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ format:
apaquarto-html: default
apaquarto-pdf:
# Can be jou (journal), man (manuscript), stu (student), or doc (document)
documentmode: man
documentmode: jou
keep-tex: true
---

```{r}
Expand All @@ -122,27 +123,24 @@ library(knitr)
conflicts_prefer(dplyr::filter, .quiet = TRUE)
conflicts_prefer(flextable::separate_header, .quiet = TRUE)
```


This is my introductory paragraph. The title will be placed above it automatically. *Do not start with an introductory heading* (e.g., "Introduction"). The title acts as your Level 1 heading for the introduction.

Details about writing headings with markdown in APA-style are [here](https://wjschne.github.io/apaquarto/writing.html#headings-in-apa-style).


## Displaying Figures

Let's make a figure. A reference label for a figure must have the prefix `fig-`, and in a code chunk, the caption must be set with `fig-cap`.
Let's make a figure. A reference label for a figure must have the prefix `fig-`, and in a code chunk, the caption must be set with `fig-cap`. Captions are in [title case](https://apastyle.apa.org/style-grammar-guidelines/capitalization/title-case).


```{r}
#| label: fig-myplot
#| fig-cap: This is the figure caption.
#| apa-note: "This is the note below the figure."
#| fig-cap: The Figure Caption
#| apa-note: This is the note below the figure.
#| fig-height: 4
#| fig-width: 6
#| apa-twocolumn: true
ggplot(data.frame(x = c(0, 35)), aes(x)) +
stat_function(fun = dchisq,
args = list(df = 10),
Expand All @@ -163,8 +161,7 @@ To refer to any figure or table, use the `@` symbol followed by the reference la
One way to import an existing graphic as a figure is to use `knitr::include_graphics` in a code chunk. For example, @fig-importedgraphic is an imported image. Note that in apaquarto-pdf documents, we can specify that that a figure or table should span both columns when in journal mode by setting the `apa-twocolumn` chunk option to `true`. For other formats, this distinction does not matter.

```{r fig-importedgraphic}
#| fig-cap: This is an imported graphic.
#| out-width: 49%
#| fig-cap: An Imported Graphic
knitr::include_graphics("img/sampleimage.png")
```

Expand All @@ -174,8 +171,8 @@ knitr::include_graphics("img/sampleimage.png")
We can make a table the same way as a figure. Generating a table that conforms to APA format in all document formats can be tricky. When the table is simple, the `kable` function from knitr works well. Feel free to experiment with different methods, but I have found that David Gohel's [flextable](https://davidgohel.github.io/flextable/) to be the best option when I need something more complex.

```{r tbl-mytable}
#| tbl-cap: Here is the table caption.
#| apa-note: Here is the note below the table.
#| tbl-cap: The Table Caption
#| apa-note: The note below the table.
#| ft.align: left
tibble(Numbers = seq(1,4), Letters = LETTERS[seq(Numbers)]) |>
flextable() %>%
Expand All @@ -198,7 +195,7 @@ In @tbl-mymarkdowntable, there is an example of a plain markdown table with a no
| 1 | 1 | 1 | 1 |


Table caption of a markdown table
Table Caption of a Markdown Table

:::

Expand Down Expand Up @@ -288,9 +285,6 @@ How are the data going to be analyzed?

Here we describe the basic characteristics of our primary variables.




<!-- Add Additional Sections as Needed -->

# Discussion
Expand Down

0 comments on commit 09bd901

Please sign in to comment.