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

Add a theme that use LaTeX fonts for everything #3147

Closed
wants to merge 18 commits into from
Closed
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Added `xreversed`, `yreversed` and `zreversed` attributes to `Axis3` [#3138](https://github.com/MakieOrg/Makie.jl/pull/3138).
- Fixed incorrect placement of contourlabels with transform functions [#3083](https://github.com/MakieOrg/Makie.jl/pull/3083)
- Fixed automatic normal generation for meshes with shading and no normals [#3041](https://github.com/MakieOrg/Makie.jl/pull/3041).
- Added a theme `theme_latexfonts` that uses the latex font family as default fonts [#3147](https://github.com/MakieOrg/Makie.jl/pull/3147).
- Added the `triplot` and `voronoiplot` recipes from DelaunayTriangulation.jl [#3102](https://github.com/MakieOrg/Makie.jl/pull/3102), [#3159](https://github.com/MakieOrg/Makie.jl/pull/3159).

## v0.19.7
Expand Down
20 changes: 20 additions & 0 deletions docs/documentation/latex.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,23 @@ Axis(f[1,1], title=L"Some %$(t) and some math: $\frac{2\alpha+1}{y}$")
f
```
\end{examplefigure}

## Uniformizing the fonts

We provide a LaTeX theme to easily switch to the LaTeX default fonts for all the text.

\begin{examplefigure}{svg = true}
```julia
using CairoMakie
CairoMakie.activate!() # hide

set_theme!(theme_latexfonts())

fig = Figure()
Label(fig[1, 1], "A standard Label", tellwidth = false)
Label(fig[2, 1], L"A LaTeXString with a small formula $x^2$", tellwidth = false)
Axis(fig[3, 1], title = "An axis with matching font for the tick labels")

fig
```
\end{examplefigure}
34 changes: 32 additions & 2 deletions docs/documentation/theming.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ Let's create a plot with the default theme:
```julia
using CairoMakie
CairoMakie.activate!() # hide

using Makie.LaTeXStrings # hide

function example_plot()
f = Figure()
for i in 1:2, j in 1:2
lines(f[i, j], cumsum(randn(50)))
end
Label(f[0, :], "A simple example plot")
Label(f[3, :], L"Random walks $x(t_n)$")
f
end

Expand All @@ -40,6 +42,7 @@ Now we define a theme which changes the default fontsize, activate it, and plot.

\begin{examplefigure}{}
```julia
using CairoMakie, Makie.LaTeXStrings # hide
fontsize_theme = Theme(fontsize = 10)
set_theme!(fontsize_theme)

Expand All @@ -53,19 +56,43 @@ This theme will be active until we call `set_theme!()`.
set_theme!()
```

## merge

Themes often only affect part of the plot attributes. Therefore it is possible to combine themes to get their respective effects together.

For example, you can combine the dark theme with the LaTeX fonts theme to have both the dark colors and uniform fonts.

\begin{examplefigure}{}
```julia
using CairoMakie, Makie.LaTeXStrings # hide
dark_latexfonts = merge(theme_dark(), theme_latexfonts())
set_theme!(dark_latexfonts)
example_plot()
```
\end{examplefigure}

## update_theme!

If you have activated a theme already and want to update it partially, without removing the attributes not in the new theme, you can use `update_theme!`.

For example, you can first call `set_theme!(my_theme)` and later update font and fontsize with `update_theme!(font = "Arial", fontsize = 18)`, leaving all other settings intact.
For example, you can decide to change the text size after activating the dark and latex theme in the previous section.

\begin{examplefigure}{}
```julia
using CairoMakie, Makie.LaTeXStrings # hide
update_theme!(fontsize=30)
example_plot()
```
\end{examplefigure}

## with_theme

Because it can be tedious to remember to switch themes off which you need only temporarily, there's the function `with_theme(f, theme)` which handles the resetting for you automatically, even if you encounter an error while running `f`.

\begin{examplefigure}{}
```julia
using CairoMakie, Makie.LaTeXStrings # hide
set_theme!() # hide
with_theme(fontsize_theme) do
example_plot()
end
Expand All @@ -76,6 +103,7 @@ You can also pass additional keywords to add or override attributes in your them

\begin{examplefigure}{}
```julia
using CairoMakie, Makie.LaTeXStrings # hide
with_theme(fontsize_theme, fontsize = 25) do
example_plot()
end
Expand All @@ -88,6 +116,7 @@ You can theme plot objects by using their uppercase type names as a key in your

\begin{examplefigure}{}
```julia
using CairoMakie, Makie.LaTeXStrings # hide
lines_theme = Theme(
Lines = (
linewidth = 4,
Expand All @@ -107,6 +136,7 @@ Here is how you could define a simple ggplot-like style for your axes:

\begin{examplefigure}{}
```julia
using CairoMakie, Makie.LaTeXStrings # hide
ggplot_theme = Theme(
Axis = (
backgroundcolor = :gray90,
Expand Down
2 changes: 2 additions & 0 deletions src/Makie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ include("themes/theme_black.jl")
include("themes/theme_minimal.jl")
include("themes/theme_light.jl")
include("themes/theme_dark.jl")
include("themes/theme_latexfonts.jl")

# camera types + functions
include("camera/projection_math.jl")
Expand Down Expand Up @@ -209,6 +210,7 @@ export theme_black
export theme_minimal
export theme_light
export theme_dark
export theme_latexfonts

export xticklabels, yticklabels, zticklabels
export xtickrange, ytickrange, ztickrange
Expand Down
10 changes: 10 additions & 0 deletions src/themes/theme_latexfonts.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function theme_latexfonts()
Theme(
fonts = Attributes(
:bold => texfont(:bold),
:bolditalic => texfont(:bolditalic),
:italic => texfont(:italic),
:regular => texfont(:regular)
)
)
end
Loading