Conditionally execute a macro depending on the font used - latex

I’m a big fan of ampersands – so much that I heed SimpleBits’ advice to “use the best available ampersand” quite religiously.
For that purpose, I’ve defined the following shortcut in LaTeX:
\let\amp\&
\renewcommand\&{{\scalebox{1.2}{\textnormal{\fontspec{Baskerville}\itshape\amp}}}}
In brief, this changes all uses of the “normal” ampersand to a stylish variant, e.g.:
This is a text \& it contains an ampersand.
(Using \& instead of just & since that’s how LaTeX works – the latter is already reserved to separate columns in table environments.)
However, this always uses the same font – here, Baskerville – no matter whether it fits or not. I’d like to use a different font depending on the font family used. That is, I want to use another ampersand in combination with sans serif text, and in particular I want to prevent rewriting of the ampersand in a monospace context. So in the following two contexts, I don’t want to trigger the above definition:
{\sffamily a \& b}
{\ttfamily a \& b}
How do I do that?
I imagine something like the following:
\renewcommand\&{
\ifsans
{\fontspec{Trebuchet MS}{\textnormal{\itshape\amp}}}
\else
\ifmono
\amp
\else
{\fontspec{Baskerville}\scalebox{1.2}{\textnormal{\itshape\amp}}}
\fi
\fi}

It works the following way:
\documentclass[letterpaper,10pt]{article}
\usepackage[latin1]{inputenc}
\usepackage{german}
\usepackage{geometry}
\geometry{margin=2cm}
\newcommand*\origsffamily{}
\let\origsffamily\sffamily
\renewcommand*\sffamily{\origsffamily\small {\renewcommand\&{{\scalebox{1.2}{\textnormal{\fontspec{Baskerville}\itshape\amp}}}}}}
\begin{document}
hello, this is \& a \sffamily test \&
\end{document}
That means, you include the change of the ampersand sign into the definition of (in my example) \sffamily.
The drawback is - of course - that you have to do this for all desired font families.

You can test for the standard LaTeX token list \f#family, however, this might not be as reliable as you want. The following code simply checks whether the current family equals the default families set by \setmainfont and \setsansfont, but not whether the fonts are really sans-serif:
\documentclass{article}
\usepackage{fontspec}
\usepackage{expl3}
\usepackage{xparse}
\setmainfont{DejaVu Serif}
\setsansfont{DejaVu Sans}
\makeatletter
\ExplSyntaxOn
\NewDocumentCommand \amp { } {
\tl_if_eq:NNTF \f#family \rmdefault {
% this is a roman font
A
} {
\tl_if_eq:NNTF \f#family \sfdefault {
% this is a sans font
B
} {
% something else
C
}
}
}
\ExplSyntaxOff
\makeatother
\begin{document}
test \amp\ test
\sffamily
test \amp\ test
\ttfamily
test \amp\ test
\end{document}

Related

Multi-language LaTeX document with dozens of languages

I'm a Technical Writer trying to output a Python-Sphinx website into a .pdf via LaTeX. The manual has a safety regulations and environmental compliance section with about 40+ languages in it. These languages all appear as-is in the base file - and .rst files have the same unicode support as .txt, so if Bulgarian renders appropriately in Cyrillic in the base file I'm assuming it's encoded correctly.
I already know to use either LuaLaTeX or XeLaTeX to render unicode properly, and I've already found that TeX files compiled from Sphinx/.rst render better under LuaLaTeX. Even so, under LuaLaTeX, the Greek and Cyrillic don't render at all (nor do accented letters, but for some reason Germanic eth/ð does render).
Everything I've seen on multi-language support involves one of several packages that require you to bracket each section with something like \begin{Russian}, but for all 40+ languages. With the base file being in a different format and the .tex file being generated automatically, every time I update the manual it would save over all the work I've done.
The best solution for me would be to put all the multi-language support in the header, and just say "hey dumb dumb... just render the unicode text as-is". As it is, the auto-generated frontspiece and ToC is unsatisfactory, so I'm keeping the header saved in a separate document and I'm pasting the better header in. Front-loading multi-language support by defining everything in the header is definitely the most ideal solution.
Any help would be good.
The following is the header provided by Python-Sphinx, with minor adjustments:
%% Generated by Sphinx.
\def\sphinxdocclass{report}
\documentclass[letterpaper,10pt,english]{sphinxmanual}
\ifdefined\pdfpxdimen
\let\sphinxpxdimen\pdfpxdimen\else\newdimen\sphinxpxdimen
\fi \sphinxpxdimen=.75bp\relax
\ifdefined\pdfimageresolution
\pdfimageresolution= \numexpr \dimexpr1in\relax/\sphinxpxdimen\relax
\fi
%% let collapsible pdf bookmarks panel have high depth per default
\PassOptionsToPackage{bookmarksdepth=5}{hyperref}
\PassOptionsToPackage{warn}{textcomp}
\usepackage[utf8]{inputenc}
\ifdefined\DeclareUnicodeCharacter
% support both utf8 and utf8x syntaxes
\ifdefined\DeclareUnicodeCharacterAsOptional
\def\sphinxDUC#1{\DeclareUnicodeCharacter{"#1}}
\else
\let\sphinxDUC\DeclareUnicodeCharacter
\fi
\sphinxDUC{00A0}{\nobreakspace}
\sphinxDUC{2500}{\sphinxunichar{2500}}
\sphinxDUC{2502}{\sphinxunichar{2502}}
\sphinxDUC{2514}{\sphinxunichar{2514}}
\sphinxDUC{251C}{\sphinxunichar{251C}}
\sphinxDUC{2572}{\textbackslash}
\fi
\usepackage{cmap}
\usepackage[T1]{fontenc}
\usepackage{amsmath,amssymb,amstext}
\usepackage{babel}
\usepackage{tgtermes}
\usepackage{tgheros}
\renewcommand{\ttdefault}{txtt}
\usepackage[Bjarne]{fncychap}
\usepackage{sphinx}
\fvset{fontsize=auto}
\usepackage{geometry}
% Include hyperref last.
\usepackage{hyperref}
% Fix anchor placement for figures with captions.
\usepackage{hypcap}% it must be loaded after hyperref.
% Set up styles of URL: it should be placed after hyperref.
\urlstyle{same}
\usepackage{sphinxmessages}
\title{...}
\date{\today}
\release{...}
\author{...}
\makeindex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
The document is almost entirely in English except for one dang section near but not at the end:
- Това е българско
- Αυτό είναι ελληνικό
- Tohle je česky
- Bu türkçe
- Þetta er íslenskt
\end{document}
Caveat: This won't give correct hyphenation and other special language settings (e.g. French spacing for punctuation marks), but it will show the text. If you want these other features as well, you will have to deal with babel or polyglossia.
The unicode capabilities of xe- and lualatex only fully unfold if you also use a font which does have a good coverage of symbols.
For example with the Noto Serif font:
% !TeX TS-program = lualatex
%% Generated by Sphinx.
\def\sphinxdocclass{report}
\documentclass[letterpaper,10pt,english]{sphinxmanual}
\ifdefined\pdfpxdimen
\let\sphinxpxdimen\pdfpxdimen\else\newdimen\sphinxpxdimen
\fi \sphinxpxdimen=.75bp\relax
\ifdefined\pdfimageresolution
\pdfimageresolution= \numexpr \dimexpr1in\relax/\sphinxpxdimen\relax
\fi
%% let collapsible pdf bookmarks panel have high depth per default
\PassOptionsToPackage{bookmarksdepth=5}{hyperref}
\PassOptionsToPackage{warn}{textcomp}
\usepackage[utf8]{inputenc}
\ifdefined\DeclareUnicodeCharacter
% support both utf8 and utf8x syntaxes
\ifdefined\DeclareUnicodeCharacterAsOptional
\def\sphinxDUC#1{\DeclareUnicodeCharacter{"#1}}
\else
\let\sphinxDUC\DeclareUnicodeCharacter
\fi
\sphinxDUC{00A0}{\nobreakspace}
\sphinxDUC{2500}{\sphinxunichar{2500}}
\sphinxDUC{2502}{\sphinxunichar{2502}}
\sphinxDUC{2514}{\sphinxunichar{2514}}
\sphinxDUC{251C}{\sphinxunichar{251C}}
\sphinxDUC{2572}{\textbackslash}
\fi
\usepackage{cmap}
\usepackage[T1]{fontenc}
\usepackage{amsmath,amssymb,amstext}
\usepackage{babel}
\usepackage{tgtermes}
\usepackage{tgheros}
\renewcommand{\ttdefault}{txtt}
\usepackage[Bjarne]{fncychap}
\usepackage{sphinx}
\fvset{fontsize=auto}
\usepackage{geometry}
% Include hyperref last.
\usepackage{hyperref}
% Fix anchor placement for figures with captions.
\usepackage{hypcap}% it must be loaded after hyperref.
% Set up styles of URL: it should be placed after hyperref.
\urlstyle{same}
\usepackage{sphinxmessages}
\title{...}
\date{\today}
\release{...}
\author{...}
\makeindex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{fontspec}
\setmainfont{Noto Serif}
\begin{document}
The document is almost entirely in English except for one dang section near but not at the end:
- Това е българско
- Αυτό είναι ελληνικό
- Tohle je česky
- Bu türkçe
- Þetta er íslenskt
\end{document}
(to see which fonts on your computer support the characters you want to use, you can use the command line tool albatross, see e.g. https://stackoverflow.com/a/69721465/2777074)

How do I solve text too wide error in LaTeX?

The text is too long and will random break my first word over the entire first line...I don't know how to make it look like the other with short text. This is my code:
\documentclass[letterpaper,11pt]{article}
\usepackage{simplecv}
\begin{document}
\vspace{1em}
\entrybig
{\textbf{LexBox}}{Nov 2020 - Present}
LexBox represents the fastest and the most efficient way to make a contravention complaint without to get in touch with a lawyer.
\\\textbf{Technologies:} C\#, ASP.NET, jQuery AJAX, CSS \& HTML
\entrybig
{\textbf{GAM - Group Activity Manager}}{Oct 2020 - Present}
{This application is designed to remote control multiple desktops at the same time. \\
\textbf{Technologies:} C\#, .NET Framework}
\end{document}
and the .sty file is this
and the result is
this
Three major problems:
you must not use \entrybig on its own, it need to be inside a list, e.g. \outerlist{}
The syntax you use for \entrybig is wrong. This macro takes 4 mandatory arguments, not 2, not 3, exactly 4
as this macro gets inserted into table cells, you must not abuse \\ for line breaks
\documentclass[letterpaper,11pt]{article}
% Choose bibliography style for formatting list of publications
\usepackage[style=ieee,url=false,doi=false,maxbibnames=99,sorting=ydnt,dashed=false]{biblatex}
\bibliography{papers}
% Choose theme, e.g. black, RedViolet, ForestGreen, MidnightBlue
\def\theme{MidnightBlue}
\usepackage{simplecv}
\begin{document}
See more of my projects on \website{https://github.com/Mihutzen}
\vspace{1em}
\outerlist{
\entrybig
{\textbf{ETH Zurich}}
{Zurich, CH}
{M.S. in Computer Science, GPA: 5.63/6.00}
{2018\textendash 2020}
}
\end{document}

\afterpage and \endfloat

How do I combine \afterpage and \endfloat to easily switch between having figures and tables at the end of the document or having them in the text?
I want to easily choose between my figures at the end of the document and my figures in the text. Because of that, sometimes I will use \afterpage package and other times I will use \endfloat would be nice to combine both.
Right now, all the times I try to run \endfloat when I have a clear page, I get the following message:
Argument of \efloat#xfloat has an extra }.
I already tried to include after page in the DeclareDelayedFloatFlavor, something like:
\DeclareDelayedFloatFlavor{afterpage}{figure}
It did not work.
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage{afterpage}
% ---------------------
%figures at the end
% ---------------------
\usepackage[nolists]{endfloat}
% force landscape at the end
\begin{document}
{\afterpage{
\begin{figure}
\end{figure}
}
\end{document}
If you want to switch between having figures within the text and at the end, I suggest to only use the endfloat package. Commenting or commenting it's optional argument disable will allow you to quickly alternate between figures at the end or in the text.
I'm not entirely certain what the purpose of afterpage was in your example, but if you used it to move the figure to a separate page, this can conveniently be done with the p floating specifier.
\documentclass{article}
\usepackage[
disable
]{endfloat}
\begin{document}
test
\begin{figure}[p]
xxx
\caption{caption}
\end{figure}
test
\end{document}

part lettering Latex

I am trying to get my document parts to show up as:
A. Narrative % \part{Narrative}
Intro %\section{Intro}
main text...
B. Appendix % \part{Appendix}
Derivations % \section{Derivations}
appendix text...
I have seen others use:
\renewcommand{\thepart}{\Alph{part}}
However this is not working for me for some reason. My parts are showing up as:
Part A
Narrative
Intro
main text...
Part B
Appendix
Derivations
appendix text...
Any ideas anyone?
The minimal example below updates \part to set its numbering differently. More specifically, it removes the \partname - Part - prefix and keeps the title on the same line. Fonts are also updated to set the part using \LARGE\bfseries in both \part and \part*. All of the above updates are done using etoolbox's \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>} macro that performs a <search>-and-<replace> within <cmd>.
\documentclass{article}
\usepackage{lipsum,etoolbox}
\renewcommand{\thepart}{\Alph{part}}
\makeatletter
% Change part display; also uniform size of \LARGE\bfseries
\patchcmd{\#part}% <cmd>
{\Large\bfseries \partname\nobreakspace\thepart \par\nobreak}% <search>
{\LARGE\bfseries \thepart.\quad}% <replace>
{}{}% <success><failure>
\patchcmd{\#part}{\huge}{\LARGE}{}{}
\patchcmd{\#spart}{\huge}{\LARGE}{}{}
\renewcommand{\#seccntformat}[1]{\csname the#1\endcsname.\quad}
% \#addtoreset{section}{part} % Reset section counter with every part
\makeatother
\begin{document}
\part{Narrative}
\section{Intro}
\lipsum[1]
\part{Appendix}
\section{Derivations}
\lipsum[2]
\end{document}
If you wish to have the \section numbers reset with every new \part, uncomment the line referencing that in the preamble.
Your idea is right, but you also redefine the titleformat.
From the following link:
\usepackage{titlesec}
\renewcommand{\thepart}{\Alph{part}}
\makeatletter
\titleformat{\part}[display]
{\Huge\scshape\filright}
{\thepart~\partname}
{20pt}
{\thispagestyle{plain}}
\makeatother

Remove section numbers latex

I have a problem with section numbering in latex. I want to remove the section numbers from headings and contents but I want the numbers to be there for lemmas, theorems etc. I want it to look like:
Section Whatever
Some unimportant text.
Section Whatever else
Another unimportant text.
Lemma 2.1
Theorem 2.2
Section Whatever again
Theorem 3.1
How can I do it? I tried
\renewcommand\thesection{}
but it removes the numbers even from lemmas and theorems. Thank you very much :)
Under the default article class we can merely remove the formatting applied to the section counter by adding
\makeatletter
\renewcommand{\#seccntformat}[1]{}
\makeatother
to the preamble. This will remove all sectional title numbering (\sections, \subsections, \subsubsections, ...), yet keep them where they are references or whenever \thesection is used.
\documentclass{article}
\newtheorem{theorem}{Theorem}[section]% Theorems numbered by section
\newtheorem{lemma}[theorem]{Lemma}% Lemma uses theorem's counter
\makeatletter
\renewcommand{\#seccntformat}[1]{}
\makeatother
\begin{document}
\section{Section whatever}
Some uninportant text.
\section{Section whatever else}
Another uninportant text.
\begin{lemma}
Some lemma.
\end{lemma}
\begin{theorem}
Some theorem.
\end{theorem}
\section{Section whatever again}
\begin{theorem}
Another theorem.
\end{theorem}
\end{document}

Resources