1

When writing a document, I usually write "topic sentences" (i.e. outline sentences) between square brackets like so:

[in here is the main idea of the next paragraph]

Here I write a polished and well written version of the topic sentence.

I would like that any text between square brackets is placed into a draft environment:

[foo bar]

should become

\begin{draft}
    foo bar
\end{draft}

The problem I am encountering is with making "[" and "]" active characters.

I understand that square brackets are normally used for arguments, and that after reading a command (i.e. \begin[args]{something}) the compiler looks for a catcode 11 character, and if it is a bracket it reads the parameters.

Changing the catcode of "[" or "]" to active (or to catcode 13) would break this.

Is there any way to go around this? Perhaps even using regex with latex3?

Thank you.

6
  • 1
    Regexes are expensive. Doing this replacement on an argument would be fine, but doing it on the entire file doesn't look like a good idea (and how should the regex know the difference between the arguments and your draft notes?). This sounds like something that might be better suited as an editor macro to do a replacement automatically if [ is the first character in the line (depends on your editor).
    – Skillmon
    Jan 26, 2023 at 17:00
  • 2
    you could use a luatex input buffer callback, otherwise I don't think there is any practical way to support that format Jan 26, 2023 at 20:39
  • Welcome to TeX.SE. Is each topic sentence contained on a single line? And how or where is the draft environment defined?
    – Mico
    Jan 26, 2023 at 21:17
  • @Skillmon I understand. I could easily replace the brackets with an editor macro (in Emacs, instance) but I would like to keep the brackets in the .tex file. Perhaps later I would use a flag to either print or not the topic sentences
    – Hugo Abreu
    Jan 26, 2023 at 22:13
  • 1
    @DavidCarlisle Thank you David, it wouldn't be problematic to use luatex so I'll take a look at that and report back.
    – Hugo Abreu
    Jan 26, 2023 at 22:14

3 Answers 3

3

If I interpret your posting correctly, you're looking to convert instances of

[ topic sentence here ]

to

\begin{draft} topic sentence here \end{draft}

where the draft environment could be defined via

\newenvironment{draft}{\color{gray}}{}

Furthermore, I will assume that topic sentences, in the input file, are on a line by themselves. Put differently, I will assume that a line that contains a topic sentence starts with [ and ends with ]. These assumptions imply that [...] in

\setmainfont{...}[...]

is not a topic sentence and hence should not be converted to \begin{draft}...\end{draft}.

If these assumptions are valid, the following LuaLaTeX-based solution may be of interest to you.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{geometry}
\usepackage{xcolor}
\newenvironment{draft}{\color{gray}}{} % per the OP's write-up

\usepackage{luacode}
\begin{luacode}
function convert_topic_sentences ( s )
  if s:find ( "^%[.+%]$" ) then -- found a topic sentence line
    return ( "\\begin{draft}"..s:sub(2,-2).."\\end{draft}" )
  end
end
\end{luacode}

% LaTeX macro to 'activate' the Lua function by assigning it to
% LuaTeX's "process_input_buffer" callback, where it acts as a 
% pre-processor.
\newcommand\TSOn{\directlua{luatexbase.add_to_callback ( 
  "process_input_buffer" , convert_topic_sentences , "TSON" )}}
\newcommand\TSOff{\directlua{luatexbase.remove_from_callback ( 
  "process_input_buffer" , "TSON" )}}

\begin{document}
\TSOn % activate the Lua function
Hello.
[A topic sentence.]
[A somewhat longer topic sentence.]
Not a topic sentence.

\TSOff % deactivate the Lua function
\smallskip
Hello.
[A topic sentence.]
[A somewhat longer topic sentence.]
Not a topic sentence.
\end{document}
5
  • Thank you! Just tested it and it does exactly what I wanted (and it doesn't add a large compilation time overhead).
    – Hugo Abreu
    Jan 26, 2023 at 23:44
  • Do you also think it would be possible to do something similar without Lua?
    – Hugo Abreu
    Jan 26, 2023 at 23:46
  • 1
    @HugoAbreu basically, no. Jan 27, 2023 at 1:23
  • @DavidCarlisle Ok thank you.
    – Hugo Abreu
    Jan 27, 2023 at 7:39
  • 1
    @HugoAbreu +1 here (you should accept this) but I posted an answer anyway on the grounds that you (or I) should never say never Jan 27, 2023 at 11:51
2

enter image description here

Doing this will probably break something, and has the extra constraint that the [ must start a paragraph (note blank line after Hello compared to Mico's example).

But if Lua is not available...

\documentclass{article}
\usepackage{geometry}
\usepackage{xcolor}
\newenvironment{draft}{\par\color{gray}}{\par} % per the OP's write-up



\makeatletter
\def\TSOn{\everypar{\dodraft}}
\def\TSOff{\everypar{}}

\def\dodraft{\@ifnextchar[\dodraft@{}}
\def\dodraft@[#1]{\begin{draft}#1\end{draft}}

\makeatother

\begin{document}
\TSOn % activate the Lua function
Hello.

[A topic sentence.]
[A somewhat longer topic sentence.]
Not a topic sentence.

\TSOff % deactivate the Lua function
\smallskip
Hello.

[A topic sentence.]
[A somewhat longer topic sentence.]
Not a topic sentence.
\end{document}
0

We don't know how your environment is defined, but I think you are looking for \lbrack and \rbrack (found in pages 102 and 103 of symbols-a4.pdf where you can found some alternatives and other symbols names.)
So, an example of definition may be \newenvironment{draft}[1]{\lbrack\textcolor{gray}{#1}\rbrack}

2
  • 1
    thank you for your answer, but I think you misunderstood the problem: I want a macro to place every occurence of [ topic sentence here ] with \begin{draft} topic sentence here \end{draft}
    – Hugo Abreu
    Jan 27, 2023 at 7:41
  • OK, I thought you wanted it in the opposite way. :D This looks like something that should be editor related, but why not?
    – gildux
    Jan 27, 2023 at 9:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .