ConTeXt

来自百合仙子's Wiki
(重定向自Context
跳转到导航 跳转到搜索

示例

% for the document info/catalog (reported by 'pdfinfo', for example)
\setupinteraction[state=start,  % make hyperlinks active, etc.
  title={Hello ConTeXt!},
  subtitle={A ConTeXt test},
  author={Lily},
  keyword={test}]

\starttext

Hello \ConTeXt!

This is my first \ConTeXt\ document, though it named c.tex. I'm about to start
learning \ConTeXt\ now, I think.

\stoptext

排版对象

空白

垂直空白:

\blank[2*big]

除了 big 外,还可以用 medium

水平空白:

% 定义空白
\definehspace[name][5em] 
% 使用空白
\hspace[name]

列表

\startitemize[a]                % tags are lowercase letters
\item first
\item second
\item third
\stopitemize

在导言区使用以下代码使标号成为粗体。 inbetween 中的字符串会插入到项目之间(竖着的)

\setupitemize[inbetween={}, style=bold]

链接

要求 \setupinteraction[state=start] ,否则链接与普通文字一样了。

\useURL[ref text][http://url][][Displaying text]

在正文里使用

\from[ref text]

交叉引用

\title[content]{目录}
...
\goto{测试回目录}[content] %使用自定义的文本
\at[somechapter] %页码
\in[somechapter] %章节计数
\about[somechapter] %文本

交叉引用中可以使用空格。

目前还只能链接到相应的页面,而不能定位到准确的位置。

公式

An equation can be typeset inline like $e^{\pi i}+1=0$, or as a
displayed formula:
\startformula
\int_0^\infty t^4 e^{-t}\,dt = 24.
\stopformula

看到其Hello World[1]上说不要使用 $$...$$ ,因为周围空间不正确[2],但是实际可用。

使用 calcmath 模块:

\usemodule[calcmath]

\calcmath{sqrt(sin^2(x)+cos^2(x))}

页面设置

正文排版

\setupindenting[always,2em,first]                  %设置中文缩进格式(首行缩进两字)
\setupwhitespace[medium]                           %设置段间距[small, medium, big]

页面

放映幻灯片时的页面大小设置:

\setuppapersize[S6][S6]

设置背景色

\setupbackgrounds[page][background=color,backgroundcolor=darkblue]

设置为 Kindle Paperwhite 的大小:[3]

\definepapersize[kpw][width=91.2mm, height=121.6mm]
\setuppapersize[kpw][kpw]

页码

不加页码:

\setuppagenumbering[state=stop]

页码的样式和位置。默认是页眉中间。如果 location 为空则没有页码。

\setuppagenumbering[location={footer,right}, style=bold]
\setuppagenumbering[location={bottom,right}]
\setuppagenumbering[location=bottom] %底部居中

根据页面大小排版

使用 \define 定义一个命令,如果页面宽度大于 130mm,则使用参数 1,否则使用参数 2:

% 引言区
\define[2]\ifwidelse{
  \ifdim\hsize>130mm
    #1
  \else
    #2
  \fi
}

% 正文区可使用:
\ifwidelse{}{\par}
\ifwidelse{\startcolumns[n=2]}{}
\ifwidelse{\stopcolumns}{}

文档设置

元信息

\setupinteraction[state=start, %使链接可以点击等
  title={title}, %标题
  subtitle={subtitle}, %主题
  author={author},
  keyword={keyword}]

交互

\setupinteractionscreen[option=max] %一开启即切换到放映模式
\setupinteractionscreen[option=bookmark] %打开书签栏

样式

字体

参见开源世界旅行手册。注意,此设置不适用于从新立得下载的ConTeXt版本。中文字体设置可使用 zhfonts 模块[4]

对齐

单个段落:

\rightaligned{右对齐文本}
\leftaligned{左对齐文本}

多段,使用 \startalignment注意参数 leftright 分别是「ragged left」和「ragged right」的意思。也可以使用 flushleftflushright 来指明「左对齐」和「右对齐」。[5]

颜色

颜色名

red, middlered, cyan, darkgreen, green, darkblue, darkcyan

定义颜色

\definecolor[a][r=0,g=0,b=1]
\definecolor[b][r=0,g=0,b=0.05]

脚本

行内脚本使用 \ctxlua ,行间脚本使用 \startlua, \stoplua

\ctxlua{a = 1.5; b = 1.8; c = a*b; tex.print(c);}

\startlua
function maximum (a)
  local mi = 1
  local m = a[mi]
  for i, val in ipairs (a) do
    if val > m then mi = i; m = val end
  end
  return m, mi
end
local v, index = maximum ({8, 10, 23, 12, 5})
tex.print (v .. "\\emdash" .. index);
\stoplua

修改PDF

[6]

% 设置元数据
\setupinteraction[
    title={测试},
    subtitle={测试修改PDF},
    author={百合仙子},
    keyword=百合; 仙子
]
 
% 设定待处理的 PDF 文档并获取其尺寸、页面数量等信息
\define\MySingleSidedDocument{test.pdf}
\getfiguredimensions[\MySingleSidedDocument]
 
% 将当前文档的页面尺寸设为与 pyramid-algorithms.pdf 的页面尺寸相等,
% \figurewidth 与 \figureheight 皆为 \getfiguredimensions 所获
\definepapersize[MyPaper][width=\figurewidth,height=\figureheight]
\setuppapersize[MyPaper]
 
% 将当前文档的天头、地脚、翻口、钉口等区域设为 0cm
\setuplayout[
    backspace=0cm,
    leftedge=0cm,
    leftedgedistance=0cm,
    rightedge=0cm,
    rightedgedistance=0cm,
    topspace=0cm,
    top=0cm,
    headerdistance=0cm,
    header=0cm,
    footerdistance=0cm,
    footer=0cm,
    topdistance=0cm,
    bottomspace=0cm,
    bottom=0cm
]
 
% 关闭页码
\setuppagenumbering[location=]
 
\starttext
 
% 将 pyramid-algorithms.pdf 文档的各页面作为图形插入到当前文档
\dorecurse\noffigurepages{%
    \externalfigure[\MySingleSidedDocument][
        page=\recurselevel,width=\paperwidth,height=\paperheight
    ]
}
 
\stoptext

使用 context --once 编译即可。

字体信息

查询已安装字体:

mtxrun --script fonts --list --all '--pattern=*'

命令

执行以下命令刷新文档数据库:

context --generate

回滚 ConTeXt MkIV 到稳定版本:

./first-setup.sh --context=current

刷新模块:

luatools --generate

刷新字体信息数据库:[7]

mtxrun --script fonts --reload

第三方模块

下载地址:http://www.ctan.org/tex-archive/macros/context/contrib/context-ruby

使用示例:

\usemodule[simplefonts]
\usemodule[ruby]
\setmainfont[umemincho]
 
\starttext
 
\ruby{}{わたし}
 
\stoptext

simplefonts

下载地址[7]http://www.ctan.org/tex-archive/macros/context/contrib/context-simplefonts

行内临时使用指定的字体[8]

\define[1]\zh{{\simplefont[simsun]#1}}
\zh{这里会使用宋体。}
TEXMFCACHE
指定缓存目录,默认是 $HOME/luatex-cache
OSFONTDIR
系统字体目录

参见

外部链接

参考资料