12 changed files with 61 additions and 89 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
Package: civilytics Package: civilytics
Type: Package Type: Package
Title: Brand Themes, Color Palettes, and Utility Functions for Civilytics Title: Brand Themes, Color Palettes, and Utility Functions for Civilytics
Version: 0.3.0 Version: 0.2.0
Authors@R: Authors@R:
person("Jared", "E. Knowles", email = "jared@civilytics.com", person("Jared", "E. Knowles", email = "jared@civilytics.com",
role = c("aut", "cre")) role = c("aut", "cre"))
+5 -3
View File
@@ -111,7 +111,9 @@ nvals <- function(x){
#' simpleCap(my_string) #' simpleCap(my_string)
simpleCap <- function(x) { simpleCap <- function(x) {
stopifnot(class(x) == "character") stopifnot(class(x) == "character")
s <- strsplit(x, " ")[[1]] sapply(x, function(word) {
paste(toupper(substring(s, 1,1)), substring(s, 2), s <- strsplit(word, " ")[[1]]
sep = "", collapse = " ") paste(toupper(substring(s, 1, 1)), substring(s, 2),
sep = "", collapse = " ")
})
} }
+7 -9
View File
@@ -135,12 +135,12 @@ use_civilytics_theme <- function(path = ".", force = FALSE) {
.copy_pkg_file(file.path("quarto/latex", f), file.path("latex", f), path, force) .copy_pkg_file(file.path("quarto/latex", f), file.path("latex", f), path, force)
} }
# Typst — shipped as template-partials so Quarto keeps its Skylighting # Typst
# definitions and syntax-highlighted code blocks render (see issue #12) .copy_pkg_file(
typst_files <- c("typst-template.typ", "typst-show.typ") "quarto/typst/civilytics-typst.typ",
for (f in typst_files) { "typst/civilytics-typst.typ",
.copy_pkg_file(file.path("quarto/typst", f), file.path("typst", f), path, force) path, force
} )
# Logos — for _brand.yml (expects assets/logo/) # Logos — for _brand.yml (expects assets/logo/)
.copy_logos("assets/logo", path, force) .copy_logos("assets/logo", path, force)
@@ -158,9 +158,7 @@ use_civilytics_theme <- function(path = ".", force = FALSE) {
message(" include-in-header: latex/civilytics.tex") message(" include-in-header: latex/civilytics.tex")
message(" include-before-body: latex/civilytics-title.tex") message(" include-before-body: latex/civilytics-title.tex")
message(" typst:") message(" typst:")
message(" template-partials:") message(" template: typst/civilytics-typst.typ")
message(" - typst/typst-template.typ")
message(" - typst/typst-show.typ")
message("---") message("---")
message("\nSee examples/report.qmd for a complete example.") message("\nSee examples/report.qmd for a complete example.")
invisible(NULL) invisible(NULL)
+13 -11
View File
@@ -36,10 +36,9 @@
#' to [civilytics_colors]`["paper_2"]` (`#F2EDE4`). #' to [civilytics_colors]`["paper_2"]` (`#F2EDE4`).
#' @param grid Character. Which major gridlines to draw: `"y"` (default, #' @param grid Character. Which major gridlines to draw: `"y"` (default,
#' horizontal only), `"x"` (vertical only), `"both"`, or `"none"`. #' horizontal only), `"x"` (vertical only), `"both"`, or `"none"`.
#' @param paper_bg Logical. If `TRUE` (default), fill the plot and panel #' @param paper_bg Logical. If `FALSE` (default), the plot and panel
#' backgrounds with the warm `paper` color. Set to `FALSE` for a #' backgrounds are transparent (`fill = NA`). Set to `TRUE` to fill them
#' transparent background (useful for slides or overlay on colored #' with the warm `paper` color (the Civilytics cream canvas).
#' surfaces).
#' #'
#' @section Font size hierarchy: #' @section Font size hierarchy:
#' All text sizes are derived from `font_size` using relative scale factors. #' All text sizes are derived from `font_size` using relative scale factors.
@@ -68,26 +67,29 @@
#' \dontrun{ #' \dontrun{
#' library(ggplot2) #' library(ggplot2)
#' #'
#' # Default editorial theme #' # Transparent background (default) — composites cleanly onto any surface
#' ggplot(mpg, aes(displ, hwy)) + #' ggplot(mpg, aes(displ, hwy)) +
#' geom_point() + #' geom_point() +
#' theme_civilytics() #' theme_civilytics()
#' #'
#' # Warm paper canvas (opt-in)
#' ggplot(mpg, aes(displ, hwy)) +
#' geom_point() +
#' theme_civilytics(paper_bg = TRUE)
#'
#' # With both gridlines and brand colors #' # With both gridlines and brand colors
#' ggplot(mpg, aes(displ, hwy, colour = class)) + #' ggplot(mpg, aes(displ, hwy, colour = class)) +
#' geom_point() + #' geom_point() +
#' scale_color_civilytics() + #' scale_color_civilytics() +
#' theme_civilytics(grid = "both") #' theme_civilytics(grid = "both")
#' #'
#' # Transparent background for embedding
#' ggplot(mpg, aes(displ, hwy)) +
#' geom_point() +
#' theme_civilytics(paper_bg = FALSE)
#'
#' # Larger text for poster or display #' # Larger text for poster or display
#' ggplot(mpg, aes(displ, hwy)) + #' ggplot(mpg, aes(displ, hwy)) +
#' geom_point() + #' geom_point() +
#' theme_civilytics(font_size = 18) #' theme_civilytics(font_size = 18)
#'
#' # Note: for fully-transparent PNGs, also set a transparent device
#' # background (e.g. `ggsave("plot.png", bg = NA)`).
#' } #' }
theme_civilytics <- function( theme_civilytics <- function(
font_size = 14, font_size = 14,
@@ -102,7 +104,7 @@ theme_civilytics <- function(
accent = unname(civilytics_colors["ember_600"]), accent = unname(civilytics_colors["ember_600"]),
strip_color = unname(civilytics_colors["paper_2"]), strip_color = unname(civilytics_colors["paper_2"]),
grid = c("y", "x", "both", "none"), grid = c("y", "x", "both", "none"),
paper_bg = TRUE) { paper_bg = FALSE) {
grid <- match.arg(grid) grid <- match.arg(grid)
half_line <- font_size / 2 half_line <- font_size / 2
+3 -3
View File
@@ -31,7 +31,7 @@ safe_max <- function(x) {
#' pretty_per(0.2, ndigit = 1) #' pretty_per(0.2, ndigit = 1)
#' pretty_per(c(0.2, 0.332423, 0.4, 0.342342), ndigit = 2) #' pretty_per(c(0.2, 0.332423, 0.4, 0.342342), ndigit = 2)
pretty_per <- function(x, ndigit = 1) { pretty_per <- function(x, ndigit = 1) {
if (any(x >= 100) & !all(is.na(x))) { if (any(x >= 100) && !all(is.na(x))) {
message("Values over 100 found, did you mean to use proportions?") message("Values over 100 found, did you mean to use proportions?")
} }
x <- format(round(x, digits = ndigit + 2) * 100, nsmall = ndigit) x <- format(round(x, digits = ndigit + 2) * 100, nsmall = ndigit)
@@ -283,7 +283,7 @@ get_stabbr <- function(fips) {
fips_codes <- fips_codes[!duplicated(fips_codes),] fips_codes <- fips_codes[!duplicated(fips_codes),]
if (length(fips) != 1) { if (length(fips) != 1) {
out <- rep(NA, length(fips)) out <- rep(NA, length(fips))
for (i in length(fips)) { for (i in seq_along(fips)) {
out[i] <- fips_codes[fips_codes$state_code == fips, 1] out[i] <- fips_codes[fips_codes$state_code == fips, 1]
} }
@@ -375,7 +375,7 @@ random_round <- function(x) {
add = rep(as.integer(0),length(r)) add = rep(as.integer(0),length(r))
add[r>test] <- as.integer(1) add[r>test] <- as.integer(1)
value = v + add value = v + add
ifelse(is.na(value) | value<0, 0, value) value <- ifelse(is.na(value) | value < 0, 0, value)
return(value) return(value)
} }
+1 -3
View File
@@ -19,9 +19,7 @@ format:
toc: true toc: true
toc-location: right toc-location: right
typst: typst:
template-partials: template: ../typst/civilytics-typst.typ
- ../typst/typst-template.typ
- ../typst/typst-show.typ
pdf: pdf:
include-in-header: ../latex/civilytics.tex include-in-header: ../latex/civilytics.tex
include-before-body: ../latex/civilytics-title.tex include-before-body: ../latex/civilytics-title.tex
+1 -1
View File
@@ -62,4 +62,4 @@ Civilytics Research, 2026
Questions? Questions?
- jared@civilytics.com - jared@civilytics.com
- civilytics.com - civilytics.consulting
+2 -5
View File
@@ -2,11 +2,6 @@
% Replaces Quarto's default \maketitle. Uses values from YAML % Replaces Quarto's default \maketitle. Uses values from YAML
% (\thetitle, \theauthor, \thedate) plus an \ifabstract block. % (\thetitle, \theauthor, \thedate) plus an \ifabstract block.
% Guard: \thesubtitle is normally defined by civilytics.tex's subtitle
% capture; provide a fallback so this partial degrades gracefully if used
% without that preamble. See civilyticsR issue #13.
\providecommand{\thesubtitle}{}
\begin{titlepage} \begin{titlepage}
\pagecolor{paper} \pagecolor{paper}
\color{ink} \color{ink}
@@ -38,6 +33,8 @@
% Pulse mark, in ember % Pulse mark, in ember
\begin{center} \begin{center}
\begin{tikzpicture}[overlay, remember picture]
\end{tikzpicture}
{\color{ember}\rule{40pt}{2pt}} {\color{ember}\rule{40pt}{2pt}}
\end{center} \end{center}
\end{titlepage} \end{titlepage}
+3 -26
View File
@@ -40,40 +40,17 @@
\color{ink} \color{ink}
% --- Fonts (require local install or fontspec lookup) --- % --- Fonts (require local install or fontspec lookup) ---
% Bold uses the family's native Bold weight (present in every Source Serif 4
% install). Do NOT hard-require a "SemiBold" face: the package installs no
% system fonts for the PDF path, and standard Source Serif 4 ships only
% Regular/Bold/Italic/BoldItalic. See civilyticsR issue #14.
\setmainfont{Source Serif 4}[ \setmainfont{Source Serif 4}[
UprightFont = *, UprightFont = *,
ItalicFont = * Italic, ItalicFont = * Italic,
BoldFont = * SemiBold,
BoldItalicFont = * SemiBold Italic,
Ligatures = TeX, Ligatures = TeX,
] ]
\setsansfont{Inter}[Ligatures = TeX] \setsansfont{Inter}[Ligatures = TeX]
\setmonofont{JetBrains Mono}[Scale = 0.92] \setmonofont{JetBrains Mono}[Scale = 0.92]
\newfontfamily\displayfont{Libre Franklin}[Ligatures = TeX] \newfontfamily\displayfont{Libre Franklin}[Ligatures = TeX]
% --- Subtitle capture ---
% Quarto/pandoc defines \subtitle (which appends to \@title) but never
% \thesubtitle, which the title page uses. This preamble is emitted before
% pandoc's \providecommand{\subtitle}, so our definition wins: capture the
% subtitle into \thesubtitle instead. See civilyticsR issue #13.
\makeatletter
\providecommand{\thesubtitle}{}
\def\subtitle#1{\renewcommand{\thesubtitle}{#1}}
\makeatother
% --- Use the Civilytics title page, not pandoc's default ---
% civilytics-title.tex (include-before-body) IS the title page. Quarto emits
% its default \maketitle + abstract *before* include-before-body, which would
% print a second, unstyled title. Neutralise both here, in the preamble
% (runs at \begin{document}, before the default title). The branded title page
% does not display the abstract. See civilyticsR issue #13.
\AtBeginDocument{%
\renewcommand{\maketitle}{}%
\renewenvironment{abstract}{\setbox0=\vbox\bgroup}{\egroup}%
}
% --- Hyperlinks --- % --- Hyperlinks ---
\hypersetup{ \hypersetup{
colorlinks = true, colorlinks = true,
@@ -100,7 +77,7 @@
\renewcommand{\footrulewidth}{0pt} \renewcommand{\footrulewidth}{0pt}
\fancyhead[L]{\sffamily\scriptsize\color{ink3}\MakeUppercase{Civilytics Consulting}} \fancyhead[L]{\sffamily\scriptsize\color{ink3}\MakeUppercase{Civilytics Consulting}}
\fancyhead[R]{\sffamily\scriptsize\color{ink3}\thetitle} \fancyhead[R]{\sffamily\scriptsize\color{ink3}\thetitle}
\fancyfoot[L]{\sffamily\scriptsize\color{ink3}civilytics.com} \fancyfoot[L]{\sffamily\scriptsize\color{ink3}civilytics.consulting}
\fancyfoot[C]{\sffamily\scriptsize\color{ink3}\thepage} \fancyfoot[C]{\sffamily\scriptsize\color{ink3}\thepage}
\fancyfoot[R]{\sffamily\scriptsize\color{ink3}\textcopyright\ 2026} \fancyfoot[R]{\sffamily\scriptsize\color{ink3}\textcopyright\ 2026}
@@ -1,15 +1,9 @@
// ============================================================= // =============================================================
// Civilytics — Typst template partial for Quarto PDF (typst-template.typ). // Civilytics — Typst template for Quarto PDF
// Shipped as a Quarto template-partial (paired with typst-show.typ) rather
// than a full `template:` so Quarto keeps its own `definitions` partial —
// which defines Skylighting/token functions needed for syntax-highlighted
// code blocks. See civilyticsR issue #12.
// Usage in YAML: // Usage in YAML:
// format: // format:
// typst: // typst:
// template-partials: // template: quarto/typst/civilytics-typst.typ
// - quarto/typst/typst-template.typ
// - quarto/typst/typst-show.typ
// ============================================================= // =============================================================
#let paper-bg = rgb("#FAF7F2") #let paper-bg = rgb("#FAF7F2")
@@ -60,7 +54,7 @@
grid( grid(
columns: (1fr, auto, 1fr), columns: (1fr, auto, 1fr),
align: (left, center, right), align: (left, center, right),
[civilytics.com], [civilytics.consulting],
counter(page).display("1 / 1", both: true), counter(page).display("1 / 1", both: true),
[© 2026] [© 2026]
) )
@@ -235,3 +229,16 @@
doc doc
} }
// Quarto entry point
#show: doc => civilytics(
title: $title$,
$if(subtitle)$subtitle: $subtitle$,$endif$
$if(by-author)$authors: ($for(by-author)$"$it.name.literal$",$endfor$),$endif$
$if(date)$date: $date$,$endif$
$if(abstract)$abstract: [$abstract$],$endif$
toc: $if(toc)$true$else$false$endif$,
doc
)
$body$
-17
View File
@@ -1,17 +0,0 @@
// Civilytics — Typst show/entry partial for Quarto (typst-show.typ).
// Pairs with typst-template.typ. Quarto appends the rendered document body
// after this partial, so this file intentionally ends with the show rule and
// no trailing body token. (Do not write that token in a comment here: Quarto
// interpolates its template variables even inside comments.)
// Title/subtitle/date are wrapped in [ ] so arbitrary text (including words
// that are Typst keywords like "for"/"in") is treated as content, not code.
// See civilyticsR issue #12.
#show: doc => civilytics(
title: [$title$],
$if(subtitle)$subtitle: [$subtitle$],$endif$
$if(by-author)$authors: ($for(by-author)$"$it.name.literal$",$endfor$),$endif$
$if(date)$date: [$date$],$endif$
$if(abstract)$abstract: [$abstract$],$endif$
toc: $if(toc)$true$else$false$endif$,
doc
)
+9 -1
View File
@@ -133,9 +133,17 @@ test_that("theme_civilytics uses brand ink color for text", {
expect_equal(th$text$colour, unname(civilytics_colors["ink"])) expect_equal(th$text$colour, unname(civilytics_colors["ink"]))
}) })
test_that("theme_civilytics uses brand paper color for plot background", { test_that("theme_civilytics has transparent background by default", {
th <- theme_civilytics() th <- theme_civilytics()
expect_true(is.na(th$plot.background$fill))
expect_true(is.na(th$panel.background$fill))
expect_null(th$legend.background$fill)
})
test_that("theme_civilytics paper_bg=TRUE opt-in fills with cream", {
th <- theme_civilytics(paper_bg = TRUE)
expect_equal(th$plot.background$fill, unname(civilytics_colors["paper"])) expect_equal(th$plot.background$fill, unname(civilytics_colors["paper"]))
expect_equal(th$panel.background$fill, unname(civilytics_colors["paper"]))
}) })
test_that("theme_civilytics uses paper_2 for strip background by default", { test_that("theme_civilytics uses paper_2 for strip background by default", {