How to use Latex to write Chinese documents in ArchLinux system? - latex

I installed the following packages according to what ArchLinux officially said:
Archlinux TexLive
$ sudo pacman -S texlive-most texlive-langchinese
I compile it like this:
\documentclass[UTF8]{ctexart}
\begin{document}
中文文档类测试。
\end{document}
$ pdflatex demo.tex
This is pdfTeX, Version 3.141592653-2.6-1.40.23 (TeX Live 2021/Arch Linux) (preloaded format=pdftex)
restricted \write18 enabled.
entering extended mode
(./demo.tex
! Undefined control sequence.
l.1 \documentclass
{ctexart}
?
! Undefined control sequence.
l.2 \begin
{document}
?
[1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}] )</usr/share/texmf-dist/
fonts/type1/public/amsfonts/cm/cmr10.pfb>
Output written on demo.pdf (1 page, 12721 bytes).
Transcript written on demo.log.
However, the result obtained is this:
Is this because I didn't add Chinese fonts? Or some other reason?

This problem is caused by my lack of understanding of the tex typesetting system.I used pdflatex directly which doesn't support Unicode character set, which is very stupid.Documents should be compiled with xelatex for the Unicode character set.

Related

How can I provide packages to tex via nix?

I'm using nix to set up a little environment I can use for latex. I want to compile a small standalone document with tikz drawing. So I have this minimal tex file:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw(0,0)circle(8);
\end{tikzpicture}
\end{document}
This should need the pgf package for tikz and the standalone package for standalone. So I have my nix.shell
{ pkgs ? import <nixpkgs> {} }:
let
tex =
pkgs.texlive.combine
{ inherit (pkgs.texlive) scheme-minimal pgf standalone;
};
in
pkgs.mkShell
{ nativeBuildInputs =
[ tex
];
}
If I run my shell and attempt to compile with pdflatex:
[nix-shell:~/Projects/Tex]$ pdflatex example.tex
This is pdfTeX, Version 3.141592653-2.6-1.40.22 (TeX Live 2021/NixOS.org) (preloaded format=pdflatex)
restricted \write18 enabled.
entering extended mode
(./example.tex
LaTeX2e <2021-11-15> patch level 1
L3 programming layer <2022-02-24>
! LaTeX Error: File `standalone.cls' not found.
It seems that latex is not aware of the packages, which isn't very surprising. But I don't really know how to do this. I've looked at texlive on the nixos wiki which doesn't say very much. They have an example which looks like what I have other than the fact it is using the home manager.
How do I make the packages installed via nix available for pdflatex?
I'm not sure why, but pdflatex is not available in scheme-minimal (even though pdftex is available). This can be verified with pure nix-shell:
$ nix-shell --pure
[nix-shell:~]$ pdflatex
The program 'pdflatex' is not in your PATH. It is provided by several packages.
You can make it available in an ephemeral shell by typing one of the following:
nix-shell -p tetex
nix-shell -p texlive.combined.scheme-basic
nix-shell -p texlive.combined.scheme-full
nix-shell -p texlive.combined.scheme-gust
nix-shell -p texlive.combined.scheme-medium
nix-shell -p texlive.combined.scheme-small
nix-shell -p texlive.combined.scheme-tetex
What happens in your case is that you run pdflatex from your main system, which does not have standalone available. If you change scheme-minimal to scheme-small, pdflatex will be available in the shell and everything will work.

Compile two versions of a document from the same latex source

How to automatically compile two versions of the same document, for example version without answers for the students and another version with answers for myself?
I have a small bash script to do a dual format.
function latex-ans () {
n=$(basename $1 .tex) # strip .tex in filename foo.tex -> foo
ln -s $n.tex $n-ans.tex # create a soft link (for instance foo-ans.tex -> foo.tex)
pdflatex '\def\withanswer{1} \input{'$n-ans'}' && pdflatex $n
% first format the version with answers and then the normal version
rm $n-ans.tex $n-ans.log
% remove useless files
}
If I have a file foo.tex, this commands formats both versions of the file and generates two pdf: foo.pdf and foo-ans.pdf. Thanks to the renaming of foo.tex through the ln -s, it also keeps separate foo.aux and foo-ans.aux to preserve useful information on both versions.
At the latex level, I basically do the same and use the macro \withanswers to configure my packages.
There are several packages that allow to conditionally exclude certain parts of the document, for example the exercise package.
With TeXstudio, the following magic comment can be used to automatically compile both versions at once (including repeated compilation for cross-references, bibliographies, indices etc.):
% !TeX program = latexmk -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" -jobname=% -pretex="\newcommand{\version}{noanswer}" -usepretex % | latexmk -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" -jobname=%_solution -pretex="\newcommand{\version}{}" -usepretex % | txs:///view-pdf "?am)_solution.pdf"
\documentclass{article}
% setting a default value in case it is compiled without the magic comment
\ifdefined\version
\else
\def\version{noanswer}
\fi
\usepackage[\version]{exercise}
\begin{document}
\begin{Exercise}[title={Title},label=ex1]
question text
\end{Exercise}
\begin{Answer}[ref={ex1}]
solution
\end{Answer}
\end{document}

Setting otherlangs from the command line using pandoc

Using pandoc, I managed to produce the following output using the YAML prolog.
---
lang: fr
otherlangs: [en]
---
Generates this latex code.
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[shorthands=off,english,main=french]{babel}
\else
\usepackage{polyglossia}
\setmainlanguage[]{french}
\setotherlanguage[]{english}
\fi
But, I cannot manage to have it working using the commande-line arguments. This what I tried.
$ pandoc -s -t latex -V lang=fr -V otherlangs="[en]"
Which produces:
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[shorthands=off,main=french]{babel}
\else
\usepackage{polyglossia}
\setmainlanguage[]{french}
\fi
Any hints on how I should pass de otherlangs variable from the command line?
AFAIK this is not possible since the -M option (and probably -V as well) parse values
as YAML boolean or string values
and otherlangs needs to be a list. What's your use case?
You shouldn't have to set otherlangs manually as it's extracted from spans and divs with the lang attribute (see the MANUAL), for example:
my [english words]{lang=en} inside a span
::: {lang=en}
followed by a native div (using this syntax since pandoc 2.0)
with another paragraph
:::
If you absolutely must set this info from the command-line, using a custom LaTeX template that contains, say, the following (if you're using Polyglossia/XeLaTeX):
\setotherlanguage[]{$myOtherLangs$}
and calling it with pandoc -V myOtherLangs="english,french" should solve your use-case.

Undefined control sequence error while converting from markdown to PDF with pandoc and xelatex

I'm making some documentation for our project, but I can't enable xelatex in pandoc to setup custom fonts. If I set --latex-engine xelatex pandoc will show that error:
pandoc: Error producing PDF from TeX source.
! Undefined control sequence.
l.17 \sys_if_engine_luatex:T
Here is a command that I run:
pandoc -s --latex-engine xelatex -f markdown build.md -o docs.pdf
I use Ubuntu 14.04 LTS, pandoc 1.12.2.1 and XeTeX, Version 3.1415926-2.5-0.9999.3 (TeX Live 2013/Debian).
Oops... after removing texlive 2013 with apt-get and installing manualy texlive 2015 pandoc will use old texlive (!!!). After that steps and setting PATH to /usr/local/texlive/2015/bin/x86_64-linux/ pandoc will finaly compile my file with xelatex.

Markdown to PDF using Pandoc since Xetex Deprecation

On my MacBook (OSX Mountain Lion), I used to use this Pandoc command to convert Markdown to PDF:
$ markdown2pdf -N -o pandoc_output.pdf --xetex --toc --template=mytemplate.tex myfile.md
But markdown2pdf no longer works, and --xetex option in markdown2pdf -N -o ../../Desktop/pandoc_output.pdf --xetex --toc --template=mytemplate-headers-garamond_date.tex is deprecated.
If I do this:
$ pandoc -N -o Desktop/pandoc_output.pdf --xetex --toc --template=mytemplate.tex myfile.md
I get this:
pandoc: unrecognized option `--xetex'
But if I take out --xetex and do this:
$ pandoc -N -o Desktop/pandoc_output.pdf --toc --template=mytemplate.tex myfile.md
then I get this:
pandoc: Error producing PDF from TeX source.
! Package hyperref Error: Wrong driver option `xetex',
(hyperref) because XeTeX is not detected.
See the hyperref package documentation for explanation.
Type H <return> for immediate help.
...
l.3925 \ProcessKeyvalOptions{Hyp}
What's the solution?
Try --latex-engine=xelatex instead of --xetex
The prior answers to this question were helpful to me, as I had installed pandoc a couple years ago, but never Tex Live. Consequently I had no idea if I had installed it correctly, so putting in the entire path helped me to see that it was working, as follows:
pandoc --latex-engine=/usr/local/texlive/2012basic/bin/universal-darwin/xelatex
This is the default install location for the BasicTex setup which you download from the Pandoc install page.
I had also forgotten about using pandoc -D Latex >my-latex-template.tex to generate a template. After giving a .tex template instead of my .html one (which caused a 'you don't have BEGIN {' error) , I got .PDF: In other words, the default template worked.
Also, I had inaccurately entered -t pdf (not shown above) to set pdf as an output format, but this was not correct. The output format is Latex, which is then translated to PDF. It is not necessary to specify an output format with the dash -t option.
I hope this record of my minor stumbles saves someone some time.
See the pandoc User's Guide (or man page) for the --latex-engine option.

Resources