Inserting Blank Spaces

Hello. I’m new to TW and was wondering how to accomplish something.

I would like to paste some of my favorite poe,s into tiddlers. Some of them have spaces at the beginning of lines, and I would like to emulate that formatting. I can paste " " for each space I would like to add, but it seems like it would be better to create a global function/procedure/macro (not sure which is most appropriate) where I can input a parameter to return " " a number of times based on the number of spaces I want. I would think it would look like this:

<<space “40”>>

The above would be replaced with 40 spaces.

Assuming this is a good way to add spaces , how would I go about defining this global function/procedure/macro?

Thanks in advance.

1 Like

Welcome to talk.tiddlywiki.org!

This should do it. There might be a simpler implementation:

\procedure space(n) <$text text={{{ [range<n>] :map[[ ]] +[join[]] }}}/>

You can test this by downloading the following and then dragging the resulting file onto any wiki (perhaps https://tiddlywiki.com/):

SpaceInvader.json (441 Bytes)

Thank you!

I tried your solution and I am having trouble calling the procedure when it’s not surrounded by a “pre” tag. Do you know what I could be doing wrong?

Ah, your problem has to do with how HTML handles spaces. It doesn’t like them. :slight_smile:

HTML collapses consecutive white-space characters (spaces, tabs, newlines, etc.) into a single space for display. (If you inspect the generated DOM, the spaces will be there.) This is very useful when writing readable HTML code, but is a problem when your spaces are meaningful. One way around this is to use a non-breaking space character. There are several ways of doing that in HTML. In TiddlyWiki, the best best is probably to use the charcode operator. We can replace the space we’re using above with charcode[160], like this:

\procedure space(n) <$text text={{{ [range<n>] :map[charcode[160]] +[join[]] }}}/>

SpaceInvader2.json (439 Bytes)

There are other good techniques involving CSS. I will try to write them up when I have a few minutes, but this might do you for now.

This works and appears to be exactly what I need. Thank you very much!

This is in no way better than the already found solution.

Just a different setup for sponges that work like mine:

\define spc(n) <$list filter="[range[$n$]]">&nbsp;</$list>

howdy<<spc 5>>there
1 Like

I disagree. It is in every way better!

It is simpler, in the objective sense that it has fewer concepts twined together. For anyone used to the Web, it uses the more common &nbsp;. It has fewer filter steps. It works in older versions of TW as well as newer ones. And if you’d prefer the more modern tools, it’s trivial to convert it to a procedure:

\procedure spaces(n) <$list filter="[range<n>]">&nbsp;</$list>

I like it!

That approach of mine is probably a poster child for how I use TiddlyWiki. Not sure if I’ve got the right analogy: 20% of the features I use handle 80% of my needs ?

(I may rethink that after a good sleep and a few cups o’ coffee tomorrow morning.)

And demonstrating the way to do it with functions, the string defaults to a Unicode em space pasted there.

\function fill(n:4 string:" ") [range[1],<n>then<string>join[]]

<<fill>>here<br>
<<fill 8>>here<br>
<<fill string:" -">>here<br>

@shakowski,

While you get a great answer by Scott, you may also have a look at https://kookma.github.io/TW-Shiraz
Open the Tutorial and look at the miscellaneous macros:
the vspace and hspace create vertical and horizontal spaces.

Example:

This is a test of <<hspace 125px>> `hspace` macro!
2 Likes

Nice Mohammad, we can also use em and % using this method :nerd_face:

Which reminds me for some cases we may want to insert spaces using the pad operator to justify a number or string with a fill.

<$list filter="[range[1,110]pad[4],[ ]]">

</$list>

again using the Unicode space

Yes, the beauty of Shiraz solution is, it accepts different CSS units e.g. px, em, cm, mm, …

2 Likes

So, if you want to retain the white-space in your poems, there are several ways to do this using CSS. Here is an extremely simple one:

title: And They Obey

@@white-space:pre-wrap;
SMASH down the cities.
Knock the walls to pieces.
Break the factories and cathedrals, warehouses
     and homes
Into loose piles of stone and lumber and black
     burnt wood:
     You are the soldiers and we command you.

Build up the cities.
Set up the walls again.
Put together once more the factories and cathedrals,
     warehouses and homes
Into buildings for life and labor:
     You are workmen and citizens all: We
     command you.
@@

This use the CSS property white-space. See the documentation for the differences between pre and pre-wrap.

Perhaps nicer is to have a stylesheet tiddler that handles Poems:

title: Poetry Stylesheet
tags: $:/tags/Stylesheet
type: text/css

[data-tags*="Poem"] .tc-tiddler-body {
  white-space: pre-wrap;
}

This adds a stylesheet with just a single selector, which captures the bodies of all tidders that have the tag Poem, and assigns the value pre-wrap to the property white-space. I would probably for non-demo give the tiddler a name inside the system namespace, perhaps something like $:/my-project/styles/poems, but for a demo Poetry Stylesheet is fine. We use it by giving a tiddler the tag Poem:

title: And They Obey
tags: Poem

SMASH down the cities.
Knock the walls to pieces.
Break the factories and cathedrals, warehouses
     and homes
Into loose piles of stone and lumber and black
     burnt wood:
     You are the soldiers and we command you.

Build up the cities.
Set up the walls again.
Put together once more the factories and cathedrals,
     warehouses and homes
Into buildings for life and labor:
     You are workmen and citizens all: We
     command you.

Again, you can download this and drag the resulting file to a wiki to see this in action:

Poetry.json (4.2 KB)

5 Likes

Thank you for the thoughtful response! I appreciate it!