|
1 | 1 | __precompile__() |
2 | 2 |
|
| 3 | +""" |
| 4 | +The LaTeXStrings module exists mainly to make LaTeX equations easier to type as |
| 5 | +literal strings, and so that the resulting strings display as formatted equations |
| 6 | +in supporting environments like IJulia. |
| 7 | +
|
| 8 | +See in particular the `LaTeXString` type and the `L"..."` constructor macro. |
| 9 | +""" |
3 | 10 | module LaTeXStrings |
4 | 11 | export LaTeXString, latexstring, @L_str, @L_mstr |
5 | | - |
6 | 12 | using Compat |
7 | | -import Compat.String |
8 | 13 |
|
9 | 14 | # IJulia supports LaTeX output for any object with a text/latex |
10 | 15 | # writemime method, but these are annoying to type as string literals |
11 | 16 | # in Julia because of all the escaping required, e.g. "\$\\alpha + |
12 | 17 | # \\beta\$". To simplify this, we add a new string type with a macro |
13 | 18 | # constructor, so that one can simply do L"$\alpha + \beta$". |
14 | 19 |
|
| 20 | +@doc raw""" |
| 21 | +A `LaTeXString` is a string type whose contents represent a fragment of LaTeX code, |
| 22 | +typically containing an equation (`$...$`). In certain environments (e.g. IJulia) |
| 23 | +this will display with LaTeX-like formatting. For the most part, you can use |
| 24 | +a `LaTeXString` object in any context that expects an `AbstractString` object. |
| 25 | +
|
| 26 | +The `L"..."` macro is convenient for constructing `LaTeXString` objects, because |
| 27 | +it eliminates the need to escape backslashes and dollar signs, and implicitly inserts |
| 28 | +dollar signs around the string if none are present. For example, `L"$\alpha$"`, `L"\alpha"`, |
| 29 | +and `LaTeXString("\$\\alpha\$")` are all equivalent. |
| 30 | +""" |
15 | 31 | struct LaTeXString <: AbstractString |
16 | 32 | s::String |
17 | 33 | end |
18 | 34 |
|
19 | | -# coercing constructor: |
| 35 | +""" |
| 36 | + latexstring(args...) |
| 37 | +
|
| 38 | +Similar to `string(args...)`, but generates a `LaTeXString` instead of a `String`. |
| 39 | +""" |
| 40 | +latexstring(args...) = latexstring(string(args...)) |
20 | 41 | function latexstring(s::String) |
21 | 42 | # the only point of using LaTeXString to represent equations, since |
22 | 43 | # IJulia doesn't support LaTeX output other than equations, so add $'s |
23 | 44 | # around the string if they aren't there (ignoring \$) |
24 | | - return ismatch(r"[^\\]\$|^\$", s) ? |
25 | | - LaTeXString(s) : LaTeXString(string("\$", s, "\$")) |
| 45 | + return occursin(r"[^\\]\$|^\$", s) ? LaTeXString(s) : LaTeXString(string('\$', s, '\$')) |
26 | 46 | end |
27 | 47 | latexstring(s::AbstractString) = latexstring(String(s)) |
28 | | -latexstring(args...) = latexstring(string(args...)) |
29 | 48 |
|
30 | 49 | macro L_str(s, flags...) latexstring(s) end |
31 | 50 | macro L_mstr(s, flags...) latexstring(s) end |
32 | 51 |
|
33 | | -import Base: write, endof, getindex, sizeof, search, rsearch, isvalid, next, length, IOBuffer, pointer |
34 | | -@compat import Base.show |
35 | | - |
36 | | -write(io::IO, s::LaTeXString) = write(io, s.s) |
37 | | -@compat show(io::IO, ::MIME"application/x-latex", s::LaTeXString) = write(io, s) |
38 | | -@compat show(io::IO, ::MIME"text/latex", s::LaTeXString) = write(io, s) |
39 | | - |
40 | | -function show(io::IO, s::LaTeXString) |
| 52 | +Base.write(io::IO, s::LaTeXString) = write(io, s.s) |
| 53 | +Base.show(io::IO, ::MIME"application/x-latex", s::LaTeXString) = write(io, s) |
| 54 | +Base.show(io::IO, ::MIME"text/latex", s::LaTeXString) = write(io, s) |
| 55 | +function Base.show(io::IO, s::LaTeXString) |
41 | 56 | print(io, "L") |
42 | 57 | Base.print_quoted_literal(io, s.s) |
43 | 58 | end |
44 | 59 |
|
45 | | -if isdefined(Base, :bytestring) |
46 | | - import Base: bytestring |
47 | | - bytestring(s::LaTeXString) = bytestring(s.s) |
| 60 | +Compat.firstindex(s::LaTeXString) = Compat.firstindex(s.s) |
| 61 | +Compat.lastindex(s::LaTeXString) = Compat.lastindex(s.s) |
| 62 | +Base.start(s::LaTeXString) = start(s.s) |
| 63 | +Base.next(s::LaTeXString, i) = next(s.s, i) |
| 64 | +Base.done(s::LaTeXString, i) = done(s.s, i) |
| 65 | +if isdefined(Base, :iterate) |
| 66 | + Base.iterate(s::LaTeXString, i::Int) = iterate(s.s, i) |
48 | 67 | end |
49 | | - |
50 | | -endof(s::LaTeXString) = endof(s.s) |
51 | | -next(s::LaTeXString, i::Int) = next(s.s, i) |
52 | | -length(s::LaTeXString) = length(s.s) |
53 | | -getindex(s::LaTeXString, i::Int) = getindex(s.s, i) |
54 | | -getindex(s::LaTeXString, i::Integer) = getindex(s.s, i) |
55 | | -getindex(s::LaTeXString, i::Real) = getindex(s.s, i) |
56 | | -getindex(s::LaTeXString, i::UnitRange{Int}) = getindex(s.s, i) |
57 | | -getindex{T<:Integer}(s::LaTeXString, i::UnitRange{T}) = getindex(s.s, i) |
58 | | -getindex(s::LaTeXString, i::AbstractVector) = getindex(s.s, i) |
59 | | -sizeof(s::LaTeXString) = sizeof(s.s) |
60 | | -search(s::LaTeXString, c::Char, i::Integer) = search(s.s, c, i) |
61 | | -rsearch(s::LaTeXString, c::Char, i::Integer) = rsearch(s.s, c, i) |
62 | | -isvalid(s::LaTeXString, i::Integer) = isvalid(s.s, i) |
63 | | -pointer(s::LaTeXString) = pointer(s.s) |
64 | | -IOBuffer(s::LaTeXString) = IOBuffer(s.s) |
65 | | - |
66 | | -import Base.convert |
67 | | -const unsafe_convert = Base.convert |
68 | | - |
69 | | -@compat unsafe_convert(T::Union{Type{Ptr{UInt8}},Type{Ptr{Int8}}}, s::LaTeXString) = convert(T, s.s) |
70 | | -unsafe_convert(::Type{Cstring}, s::LaTeXString) = unsafe_convert(Cstring, s.s) |
| 68 | +Base.nextind(s::LaTeXString, i::Int) = nextind(s.s, i) |
| 69 | +Base.prevind(s::LaTeXString, i::Int) = prevind(s.s, i) |
| 70 | +Base.eachindex(s::LaTeXString) = eachindex(s.s) |
| 71 | +Base.length(s::LaTeXString) = length(s.s) |
| 72 | +Base.getindex(s::LaTeXString, i::Integer) = getindex(s.s, i) |
| 73 | +Base.getindex(s::LaTeXString, i::Int) = getindex(s.s, i) # for method ambig in Julia 0.6 |
| 74 | +Base.getindex(s::LaTeXString, i::UnitRange{Int}) = getindex(s.s, i) |
| 75 | +Base.getindex(s::LaTeXString, i::UnitRange{<:Integer}) = getindex(s.s, i) |
| 76 | +Base.getindex(s::LaTeXString, i::AbstractVector{<:Integer}) = getindex(s.s, i) |
| 77 | +Compat.codeunit(s::LaTeXString, i::Integer) = codeunit(s.s, i) |
| 78 | +Compat.codeunit(s::LaTeXString) = codeunit(s.s) |
| 79 | +Compat.ncodeunits(s::LaTeXString) = ncodeunits(s.s) |
| 80 | +Compat.codeunits(s::LaTeXString) = codeunits(s.s) |
| 81 | +Base.sizeof(s::LaTeXString) = sizeof(s.s) |
| 82 | +Base.isvalid(s::LaTeXString, i::Integer) = isvalid(s.s, i) |
| 83 | +Base.pointer(s::LaTeXString) = pointer(s.s) |
| 84 | +Base.IOBuffer(s::LaTeXString) = IOBuffer(s.s) |
| 85 | +Base.unsafe_convert(T::Union{Type{Ptr{UInt8}},Type{Ptr{Int8}},Cstring}, s::LaTeXString) = Base.unsafe_convert(T, s.s) |
71 | 86 |
|
72 | 87 | end # module |
0 commit comments