13

This is what I want:

  1. When I type something between ``, it should turn bold and green.
  2. When I type Q:, the text following it till it meets a ? should turn italics and blue.

Can this kind of stuff be achieved through Latex? If yes, then please guide by pointing to relevant documents of Latex.

In reply to this comment: Is Latex useful for custom formatting?

What is your actual goal? Do you want to create an application that generates a PDF or do you want to write LaTeX plus these new features?

My goal is to type the following content with the shown formatting and colors in a modifiable document:
enter image description here

8
  • 1
    Maybe this answer can help you: tex.stackexchange.com/a/401767/134144
    – leandriis
    Dec 21, 2017 at 9:13
  • Do you use pdfLaTeX? Is switching to LuaLaTeX an option for you? Please also provide more details about your formatting needs. E.g. should just the material between the quotation marks be rendered in green and bold, or should this also pertain to the quotation marks? Should the quotation marks be shown? For the Q: ... ? strings: should Q and ? be rendered in blue and italics as well?
    – Mico
    Dec 21, 2017 at 9:46
  • 5
    What is your actual goal? Do you want to create an application that generates a PDF or do you want to write LaTeX plus these new features?
    – Pål GD
    Dec 21, 2017 at 12:22
  • 11
    What is the XY problem?
    – Pål GD
    Dec 21, 2017 at 12:23
  • 2
    I think that some kind of markdown language will be more useful for this kind of application. I do not know how to customize it (probably some css magic )...
    – Rmano
    Dec 22, 2017 at 8:25

3 Answers 3

27

Here's a LuaLaTeX-based solution.

enter image description here

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}
9
  • 4
    @KeksDose be careful what you ask for... \AtBegi{\color{green} n }Docume{\color{green} n }t Dec 21, 2017 at 11:26
  • 3
    @KeksDose - The preprocessor-based approach proposed here should work fine with strings of the type mentioned in the OP's query. It's bound to break down -- see David Carlisle's example -- for more generic queries, such as "make every instance of the letter n green". If you want a solution to the more generic queries, please post a new query.
    – Mico
    Dec 21, 2017 at 12:25
  • 2
    @KeksDose - Regarding your first question, about how much slower compilation may be: There are two separate things to consider: First, LuaTeX is more complex than pdfTeX, i.e., it can handle much more complicated things than pdfTeX can. A cost is that compilation under LuaTeX will almost invariably slower than the corresponding compilation under pdfTeX. Second, the overhead imposed by the two string.gsub search-and-replace directives is likely quite low, as nether of the string.gsub operation is all that complicated.
    – Mico
    Dec 21, 2017 at 13:12
  • 1
    @Mico Thank you! I'll ask about painting all "n"s green. But not today.
    – Keks Dose
    Dec 21, 2017 at 13:14
  • 2
    @Aquarius_Girl - You're welcome. The Lua part in LuaLaTeX refers to Lua, the modern programming language that's derived from "C". LuaTeX is the most modern of the three main TeX engines, the other two being pdfTeX and XeTeX. One of several things I love about LuaLaTeX is that combines the best of (a) LaTeX and (b) Lua, with a straightforward interface between them (mostly via \directlua and the luacode package.) To answer your questions: I'm not aware of any software that would somehow "generate the code" which I wrote. Fortunately, it doesn't take much experience in Lua to get started.
    – Mico
    Dec 22, 2017 at 6:49
29

a bang for a buck pictureI'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}
4
  • Shouldn't it be `#1'?
    – wizzwizz4
    Dec 22, 2017 at 15:15
  • @wizzwizz4 No, please have a look at the question!
    – Keks Dose
    Dec 22, 2017 at 15:25
  • I think the question's asking for the conversion of `text` to green text, not text to green `text`. See the image in the question.
    – wizzwizz4
    Dec 22, 2017 at 15:37
  • @wizzwizz4 Probably you are right. But it is so easy to change that for any user. Not worth to change the code here and upload a new screenshot.
    – Keks Dose
    Dec 22, 2017 at 17:14
21

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}

Result


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.

3
  • Thanks for the effort to you too. I have zero experience in Latex. I wish to know that the code that you have written here is something that a user is expected to write himself? Or is there some software of Latex where the user would have to specify 'Q' and '``' and expect the software to generate the code which you have written? Have asked the same thing to the other answerer too. Not sure if he would reply so I am asking this to you too. Dec 22, 2017 at 4:41
  • 3
    @Aquarius_Girl: As I mentioned, this is advanced, programming stuff using TeX. Note that LaTeX is more or less a userinterface above TeX, which defines a lot of often used macros like \chapter and \section to easy the use. I don't know any tool which generates such code, but often there are already programmed packages which extend (La)TeX so that you can call some macro to define special meaning. The quote character for example can be defined using the newverbs package. I will add some example code. Dec 22, 2017 at 6:38
  • Thanks much for a detailed response. As I understand now that such things are possible in Latex, it is just that we are supposed to write the code for it the way you have written. Dec 22, 2017 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.