@@ -3,34 +3,20 @@ description: Typstチュートリアル
33---
44
55# テンプレートを作成する
6- In the previous three chapters of this tutorial, you have learned how to write a
7- document in Typst, apply basic styles, and customize its appearance in-depth to
8- comply with a publisher's style guide. Because the paper you wrote in the
9- previous chapter was a tremendous success, you have been asked to write a
10- follow-up article for the same conference. This time, you want to take the style
11- you created in the previous chapter and turn it into a reusable template. In
12- this chapter you will learn how to create a template that you and your team can
13- use with just one show rule. Let's get started!
14-
15- ## A toy template { #toy-template }
16- In Typst, templates are functions in which you can wrap your whole document. To
17- learn how to do that, let's first review how to write your very own functions.
18- They can do anything you want them to, so why not go a bit crazy?
6+ このチュートリアルの前回の3つの章では、Typstでドキュメントを書く方法、基本的なスタイルを適用する方法、そして出版社のスタイルガイドに準拠するために外観を詳細にカスタマイズする方法を学びました。前章で作成した論文が大成功を収めたため、同じ会議のための続報論文を書くよう依頼されました。今回は、前章で作成したスタイルを再利用可能なテンプレートに変換したいと思います。この章では、あなたとあなたのチームが単一のshowルールで使用できるテンプレートの作成方法を学びます。始めましょう!
7+
8+ ## おもちゃのテンプレート { #toy-template }
9+ Typstでは、テンプレートは文書全体をラップできる関数です。その方法を学ぶために、まずは独自の関数の書き方を復習しましょう。関数は何でもできるので、少し奇抜なものを作ってみませんか?
1910
2011``` example
2112#let amazed(term) = box[✨ #term ✨]
2213
2314You are #amazed[beautiful]!
2415```
2516
26- This function takes a single argument, ` term ` , and returns a content block with
27- the ` term ` surrounded by sparkles. We also put the whole thing in a box so that
28- the term we are amazed by cannot be separated from its sparkles by a line break.
17+ この関数は単一の引数` term ` を取り、` term ` を✨で囲んだコンテンツブロックを返します。また、amazed対象の語が改行で✨と分離されないように、全体をボックスに入れています。
2918
30- Many functions that come with Typst have optional named parameters. Our
31- functions can also have them. Let's add a parameter to our function that lets us
32- choose the color of the text. We need to provide a default color in case the
33- parameter isn't given.
19+ Typstに組み込まれている多くの関数には、オプションの名前付きパラメータがあります。私たちの関数にも名前付きパラメータを追加できます。テキストの色を選択できるパラメータを追加してみましょう。パラメータが指定されない場合のデフォルトの色を提供する必要があります。
3420
3521``` example
3622#let amazed(term, color: blue) = {
@@ -41,14 +27,7 @@ You are #amazed[beautiful]!
4127I am #amazed(color: purple)[amazed]!
4228```
4329
44- Templates now work by wrapping our whole document in a custom function like
45- ` amazed ` . But wrapping a whole document in a giant function call would be
46- cumbersome! Instead, we can use an "everything" show rule to achieve the same
47- with cleaner code. To write such a show rule, put a colon directly behind the
48- show keyword and then provide a function. This function is given the rest of the
49- document as a parameter. The function can then do anything with this content.
50- Since the ` amazed ` function can be called with a single content argument, we can
51- just pass it by name to the show rule. Let's try it:
30+ テンプレートは` amazed ` のようなカスタム関数でドキュメント全体をラップすることで機能します。しかし、文書全体を巨大な関数呼び出しでラップするのは面倒でしょう!代わりに、「everything」showルールを使用して、より洗練されたコードで同じことを実現できます。そのようなshowルールを書くには、showキーワードの直後にコロンを置き、関数を提供します。この関数にはドキュメントの残りの部分がパラメータとして渡されます。関数はこのコンテンツに対して何でも行うことができます。` amazed ` 関数は単一のコンテンツ引数で呼び出せるので、showルールに名前で渡すだけで良いのです。試してみましょう。
5231
5332``` example
5433>>> #let amazed(term, color: blue) = {
@@ -61,15 +40,10 @@ negative thoughts or beliefs.
6140In fact, I am amazing!
6241```
6342
64- Our whole document will now be passed to the ` amazed ` function, as if we wrapped
65- it around it. Of course, this is not especially useful with this particular
66- function, but when combined with set rules and named arguments, it can be very
67- powerful.
43+ これで文書全体が` amazed ` 関数に渡され、文書をその関数でラップしたかのように機能します。もちろん、この特定の関数ではあまり有用ではありませんが、setルールと名前付き引数と組み合わせると、非常に強力になります。
6844
69- ## Embedding set and show rules { #set-and-show-rules }
70- To apply some set and show rules to our template, we can use ` set ` and ` show `
71- within a content block in our function and then insert the document into
72- that content block.
45+ ## setルールとshowルールの埋め込み { #set-and-show-rules }
46+ テンプレートにいくつかのsetルールとshowルールを適用するには、関数内のコンテンツブロックで` set ` と` show ` を使用し、そのコンテンツブロックにドキュメントを挿入します。
7347
7448``` example
7549#let template(doc) = [
@@ -83,12 +57,7 @@ I am learning something cool today.
8357It's going great so far!
8458```
8559
86- Just like we already discovered in the previous chapter, set rules will apply to
87- everything within their content block. Since the everything show rule passes our
88- whole document to the ` template ` function, the text set rule and string show
89- rule in our template will apply to the whole document. Let's use this knowledge
90- to create a template that reproduces the body style of the paper we wrote in the
91- previous chapter.
60+ 前章で発見したように、setルールはそのコンテンツブロック内のすべてに適用されます。everythingのshowルールが文書全体を` template ` 関数に渡すため、テンプレート内のテキストのsetルールと文字列のshowルールが文書全体に適用されます。この知識を使って、前章で作成した論文の本文スタイルを再現するテンプレートを作成しましょう。
9261
9362``` example
9463#let conf(title, doc) = {
@@ -154,31 +123,16 @@ previous chapter.
154123>>> #lorem(200)
155124```
156125
157- We copy-pasted most of that code from the previous chapter. The two differences
158- are this:
126+ コードの大部分は前章からコピーペーストしました。2つの違いがあります。
159127
160- 1 . We wrapped everything in the function ` conf ` using an everything show rule.
161- The function applies a few set and show rules and echoes the content it has
162- been passed at the end.
128+ 1 . everythingのshowルールを使用して、すべてを` conf ` 関数でラップしました。この関数はいくつかのsetルールとshowルールを適用し、最後に渡されたコンテンツをそのまま出力します。
163129
164- 2 . Moreover, we used a curly-braced code block instead of a content block. This
165- way, we don't need to prefix all set rules and function calls with a ` # ` . In
166- exchange, we cannot write markup directly in the code block anymore.
130+ 2 . さらに、コンテンツブロックの代わりに中括弧で囲まれたコードブロックを使用しました。この方法では、すべてのsetルールや関数呼び出しの前に` # ` を付ける必要がなくなります。代わりに、コードブロック内に直接マークアップを書くことはできなくなります。
167131
168- Also note where the title comes from: We previously had it inside of a variable.
169- Now, we are receiving it as the first parameter of the template function. To do
170- so, we passed a closure (that's a function without a name that is used right
171- away) to the everything show rule. We did that because the ` conf ` function
172- expects two positional arguments, the title and the body, but the show rule will
173- only pass the body. Therefore, we add a new function definition that allows us
174- to set a paper title and use the single parameter from the show rule.
132+ また、タイトルがどこから来ているかに注目してください。以前は変数に格納しましたが、今はテンプレート関数の最初のパラメータとして受け取っています。そのために、everythingのshowルールにクロージャー(その場で使用される名前のない関数)を渡しました。` conf ` 関数は2つの引数(タイトルと本文)を期待しますが、showルールは本文のみを渡すからです。したがって、論文のタイトルを設定し、showルールからの単一パラメータを使用できる新しい関数定義を追加します。
175133
176- ## Templates with named arguments { #named-arguments }
177- Our paper in the previous chapter had a title and an author list. Let's add
178- these things to our template. In addition to the title, we want our template to
179- accept a list of authors with their affiliations and the paper's abstract. To
180- keep things readable, we'll add those as named arguments. In the end, we want it
181- to work like this:
134+ ## 名前付き引数を持つテンプレート { #named-arguments }
135+ 前章の論文にはタイトルと著者リストがありました。これらの要素をテンプレートに追加しましょう。タイトルに加えて、所属機関を含む著者リストと論文の要約をテンプレートに受け付けるようにします。可読性を保つために、これらを名前付き引数として追加します。最終的には、次のように機能させたいと思います。
182136
183137``` typ
184138#show: doc => conf(
@@ -202,35 +156,13 @@ to work like this:
202156...
203157```
204158
205- Let's build this new template function. First, we add a default value to the
206- ` title ` argument. This way, we can call the template without specifying a title.
207- We also add the named ` authors ` and ` abstract ` parameters with empty defaults.
208- Next, we copy the code that generates title, abstract and authors from the
209- previous chapter into the template, replacing the fixed details with the
210- parameters.
211-
212- The new ` authors ` parameter expects an [ array] ( $array ) of [ dictionaries] ( $dictionary )
213- with the keys ` name ` , ` affiliation ` and ` email ` . Because we can have an
214- arbitrary number of authors, we dynamically determine if we need one, two or
215- three columns for the author list. First, we determine the number of authors
216- using the [ ` .len() ` ] ( $array.len ) method on the ` authors ` array. Then, we set the
217- number of columns as the minimum of this count and three, so that we never
218- create more than three columns. If there are more than three authors, a new row
219- will be inserted instead. For this purpose, we have also added a ` row-gutter `
220- parameter to the ` grid ` function. Otherwise, the rows would be too close
221- together. To extract the details about the authors from the dictionary, we use
222- the [ field access syntax] ( $scripting/#fields ) .
223-
224- We still have to provide an argument to the grid for each author: Here is where
225- the array's [ ` map ` method] ( $array.map ) comes in handy. It takes a function as an
226- argument that gets called with each item of the array. We pass it a function
227- that formats the details for each author and returns a new array containing
228- content values. We've now got one array of values that we'd like to use as
229- multiple arguments for the grid. We can do that by using the
230- [ ` spread ` operator] ( $arguments ) . It takes an array and applies each of its items
231- as a separate argument to the function.
232-
233- The resulting template function looks like this:
159+ この新しいテンプレート関数を構築しましょう。まず、` title ` 引数にデフォルト値を追加します。これにより、タイトルを指定せずにテンプレートを呼び出すことができます。また、空のデフォルト値を持つ名前付き引数として` authors ` および` abstract ` パラメータを追加します。次に、前章からタイトル、要約、著者を生成するコードをテンプレートにコピーし、固定の詳細をパラメータに置き換えます。
160+
161+ 新しい` authors ` パラメータは、` name ` 、` affiliation ` 、` email ` というキーを持つ[ 辞書] ( $dictionary ) の[ 配列] ( $array ) を想定しています。任意の数の著者を持つことができるため、著者リストに1列、2列、または3列が必要かどうかを動的に決定します。まず、` authors ` 配列の[ ` .len() ` ] ( $array.len ) メソッドを使用して著者の数を決定します。次に、列数を著者数と3の最小値に設定し、3列以上作成しないようにします。3人以上の著者がいる場合は、代わりに新しい行が挿入されます。この目的のために、` grid ` 関数に` row-gutter ` パラメータも追加しました。そうしないと、行同士が近すぎてしまいます。辞書から著者の詳細を抽出するには、[ フィールドアクセス構文] ( $scripting/#fields ) を使用します。
162+
163+ 各著者についてグリッドに引数を提供する必要があります。ここで配列の[ ` map ` メソッド] ( $array.map ) が便利です。これは引数として関数を取り、その関数が配列の各アイテムで呼び出されます。各著者の詳細をフォーマットし、コンテンツ値を含む新しい配列を返す関数を渡します。これで、グリッドの複数の引数として使用したい値の配列ができました。[ ` spread ` 演算子] ( $arguments ) を使用してこれを実現できます。これは配列を取り、その各アイテムを関数の個別の引数として適用します。
164+
165+ 結果のテンプレート関数は次のようになります。
234166
235167``` typ
236168#let conf(
@@ -268,21 +200,10 @@ The resulting template function looks like this:
268200}
269201```
270202
271- ## A separate file { #separate-file }
272- Most of the time, a template is specified in a different file and then imported
273- into the document. This way, the main file you write in is kept clutter free and
274- your template is easily reused. Create a new text file in the file panel by
275- clicking the plus button and name it ` conf.typ ` . Move the ` conf ` function
276- definition inside of that new file. Now you can access it from your main file by
277- adding an import before the show rule. Specify the path of the file between the
278- ` {import} ` keyword and a colon, then name the function that you want to import.
279-
280- Another thing that you can do to make applying templates just a bit more elegant
281- is to use the [ ` .with ` ] ( $function.with ) method on functions to pre-populate all
282- the named arguments. This way, you can avoid spelling out a closure and
283- appending the content argument at the bottom of your template list. Templates on
284- [ Typst Universe] ( $universe ) are designed to work with this style of function
285- call.
203+ ## 別ファイル { #separate-file }
204+ 多くの場合、テンプレートは別のファイルで指定され、それからドキュメントにインポートされます。この方法では、メインファイルはすっきりとし、テンプレートを簡単に再利用できます。ファイルパネルでプラスボタンをクリックして新しいテキストファイルを作成し、` conf.typ ` という名前を付けます。` conf ` 関数定義をその新しいファイルに移動します。これで、showルールの前にインポートを追加することで、メインファイルからアクセスできます。` {import} ` キーワードとコロンの間にファイルのパスを指定し、インポートしたい関数に名前を付けます。
205+
206+ テンプレートの適用をより洗練させるためにできるもう1つのことは、関数の[ ` .with ` ] ( $function.with ) メソッドを使用して、すべての名前付き引数を事前に設定することです。これにより、クロージャーを記述してテンプレートリストの最後にコンテンツ引数を追加する必要がなくなります。[ Typst Universe] ( $universe ) のテンプレートは、この関数呼び出しのスタイルで動作するように設計されています。
286207
287208``` example:single
288209>>> #let conf(
@@ -385,22 +306,11 @@ call.
385306#lorem(200)
386307```
387308
388- We have now converted the conference paper into a reusable template for that
389- conference! Why not share it in the [ Forum] ( https://forum.typst.app/ ) or on
390- [ Typst's Discord server] ( https://discord.gg/2uDybryKPe ) so that others can use
391- it too?
392-
393- ## Review
394- Congratulations, you have completed Typst's Tutorial! In this section, you have
395- learned how to define your own functions and how to create and apply templates
396- that define reusable document styles. You've made it far and learned a lot. You
397- can now use Typst to write your own documents and share them with others.
398-
399- We are still a super young project and are looking for feedback. If you have any
400- questions, suggestions or you found a bug, please let us know
401- in the [ Forum] ( https://forum.typst.app/ ) ,
402- on our [ Discord server] ( https://discord.gg/2uDybryKPe ) ,
403- on [ GitHub] ( https://github.com/typst/typst/ ) ,
404- or via the web app's feedback form (always available in the Help menu).
405-
406- So what are you waiting for? [ Sign up] ( https://typst.app ) and write something!
309+ これで会議論文を、その会議用の再利用可能なテンプレートに変換しました![ フォーラム] ( https://forum.typst.app/ ) や[ TypstのDiscordサーバー] ( https://discord.gg/2uDybryKPe ) で共有して、他の人も使えるようにしてみてはいかがでしょうか?
310+
311+ ## まとめ { #review }
312+ おめでとうございます!Typstのチュートリアルを完了しました。このセクションでは、独自の関数を定義する方法と、再利用可能なドキュメントスタイルを定義するテンプレートを作成・適用する方法を学びました。あなたは多くを学び、ここまで来ました。これでTypstを使用して独自の文書を作成し、他の人と共有することができます。
313+
314+ 私たちはまだ非常に若いプロジェクトであり、フィードバックを求めています。質問、提案、またはバグを発見した場合は、[ フォーラム] ( https://forum.typst.app/ ) 、[ Discordサーバー] ( https://discord.gg/2uDybryKPe ) 、[ GitHub] ( https://github.com/typst/typst/ ) 、またはウェブアプリのフィードバックフォーム(ヘルプメニューからいつでも利用可能)でお知らせください。
315+
316+ さっそく[ サインアップ] ( https://typst.app ) して何か書いてみましょう!
0 commit comments