Is Latex useful for custom formatting?
I'd say wrong approach. What you ask needs low level programming in TeX internals. You'll see that in this answer, as linked in a comment by leandriis.
You can have it as simple as here:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\newcommand{\bang}[1]{\textcolor{green}{\textbf{`#1`}}}
\newcommand{\Q}[1]{\emph{\textcolor{blue}{Q#1?}}}
\begin{document}
Always keep things as simple as possible. A \bang{bang for a
buck}. More \Q{ualifications are needed if you mess things up}
\end{document}
Here's a LuaLaTeX-based solution.
This solution takes a preprocessor approach. Lua has several very powerful and flexible pattern-matching functions. The Lua function make_replacements
performs two pattern-matching-and-substitution operations on all input material, before TeX even begins its "regular" processing.
Addendum to incorporate the additional information provided by the OP regarding his/her typesetting objectives:
LaTeX is very well suited to handle such typesetting needs. I suggest you (a) define the following macro in the preamble
\newcommand\QQ[1]{\par\noindent Q: {\itshape\textcolor{green}{#1}}\par}
and then write something like
\QQ{What is a pointer?}
or
\QQ{Where are expressions, constants stored if not in memory?}
in the body of the text. Similarly, you'd provide an \AA
macro that provides the basic formatting of the answers.
Here's the code that generates the screenshot shown above.
% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{xcolor,luacode}
%% Lua-side code
\begin{luacode}
function make_replacements ( s )
s = string.gsub ( s , "Q:(.-)%?" , "Q: {\\itshape\\color{blue}%1}?" )
s = string.gsub ( s , "``(.-)''" , "``{\\bfseries\\color{green}%1}''" )
return s
end
\end{luacode}
%% TeX-side code
\AtBeginDocument{\directlua{luatexbase.add_to_callback (
"process_input_buffer" , make_replacements , "make_replacements" )}}
\begin{document}
bla bla Q: Yes? bla bla ``No'' More bla bla
\end{document}
TeX (and therefore LaTeX) allows you to redefine its parsing settings. This way specific characters can be made "active", i.e. they work like macros then. Also it allows you to specify an argument text which is searched when expanding macros. This way you can implement things like "read everything to the next '?'". However, you need to use plainTeX macros (e.g. \def
instead of \newcommand
). The best reference is "The TeXBook" by Knuth, but there are others (e.g. "TeX by reference"). I'm also using \@ifnextchar
, which is provided by LaTeX, to look ahead. Such macros are listed in macros2e
. It uses plainTeX's \futurelet
however.
Note that this is advanced stuff which may cause errors if used incorrectly. Better would be to add macros yourself around the content. An alternative would be a script which converts the text to LaTeX by scanning for the patterns you described and added LaTeX macros around it automatically.
Note when a space follows the Q
it is removed. This is due to \@ifnextchar
. Also, you can no longer use Q
inside macro names. I'm getting an error at \begin{document}
as some other package tries to use the Q
in an macro argument there. Therefore I moved the code after it.
\documentclass{article}
\usepackage{xcolor}
\begin{document}
\makeatletter
\def\old@q{Q}
\catcode`\Q=\active
\def Q{%
\@ifnextchar :{%
\process@q
}{%
\old@q
}%
}
\def\process@q :#1?{%
{\color{blue}\itshape #1}%
}
\makeatother
\catcode`\`=\active
\long\def`#1`{{\color{green}\bfseries #1}}
Test `it` out
Q: test it?
AA Q AA
Quick test
\end{document}
The blue italic quotes can be implemented very nicely using the newverbs
package. Just define a new verb command and then attach it to a character as "short verb".
\usepackage{newverbs}
\newverbcommand{\qblueitalic}{\begingroup\color{blue}\itshape\qverbbeginquote}{\qverbendquote\endgroup}
\MakeSpecialShortVerb{\qblueitalic}{\`}
The \qverbbeginquote
and \qverbendquote
macros readd the normal quote characters as normal text.