Formatting asterisks in text and callouts

My use case is that as a librarian, I often write out search strategies in my notes for research consultations. Search syntax includes asterisks for truncation, which presents a formatting issue in Obsidian. It doesn’t break anything, but it’s a little bit inconvenient with my current setup.

Things I have tried

The two workarounds I have found from searching around are using \ to escape the asterisk, and enclosing the search string in backticks. Using \ works fine, until I have to copy/paste the search string to share with the patron and then have to manually delete those characters. Using backticks is a more elegant solution, but I have a purely aesthetic quibble with how codeblocks look inside of callouts, which are where I typically put search strategies.

What I’m trying to do

I can think of two potential solutions, but I’m not savvy enough to implement them on my own.

  1. Instead of escaping the character, is there a way to tell Obsidian to ignore pairs of asterisks for formatting purposes? Currently, if there are two truncated search terms on a line, it will format everything between them as italics without the \ character. Can I possibly turn this off so that anything between a pair of asterisks is rendered as normal text? I can easily adapt to using underscores for italics formatting instead. This would be the ideal solution, as I also sometimes have a couple truncated terms in my notes that I would rather not put in a code block.
  2. Alternatively, is there a way to format code blocks inside of callouts so that they do not have the gray background? Ideally, code blocks outside of callouts would still have the gray background, but if they are inside a callout, the callout background would remain instead. Right now I really don’t like the way it looks to have a code block inside a callout (see image).

I’m pretty sure that the second solution can be done with CSS, but my CSS is extremely limited and I have enough actual code in my vault that I don’t want to mess with it and break anything else.

Thank you!

I don’t think there’s a way to tell Obsidian to ignore asterisks for formatting purposes. Sorry, to say so. So they would need to be escaped, unless within code block or similar. Some renderers handle this situation by checking whether the asterisk (or underline for that matter) is at the start, in or end of word, and it only gets their formatting meaning if there is a pair of “at start of word” and “at end of word”.

In your case, that would have solved the issue as you’ve got two “at end of word”-asterisks. Whether this is to be defined as a bug or not, that’s a different story.

Regarding the second option, what kind of callouts are you using? Admonition style, or ordinary callouts (using > [!info] or similar) ?

If using ordinary callouts, you could use some CSS like the following:

.callout pre {
  background-color: inherit;
  border: 2px inset gray;
} 

I’d strongly suggest to add some borders around the code block to make it standout a little from the surrounding text, but the key part of this CSS is of course the background-color: inherit, which makes it adapt to the other background color.

It really doesn’t have to be a Catch-22 situation, yossarian.

If you use a Mac, open Edit > Emoji & Symbols and search for asterisk. Find an asterisk you like, such as the asterisk operator (∗), and insert it into a file.

Select and copy that asterisk and go to Apple > System Settings > Keyboard > Text Replacements > and then click the + sign at the bottom of the pop-up window to create a new replacement. In Replace, type a unique keystroke (I use .a.) and then in With, paste the asterisk symbol you chose above. Quit out of settings to make sure the replacement is saved to the system.

Now, whenever you need an asterisk that won’t format anything in markdown, just use your replacement keystroke. You should get an asterisk that works in Obsidian and should (do test this) copy and paste to other formats. The keystroke should also work across most Mac apps.

If you don’t use a Mac, I hope you can do something similar on whatever platform you do use.

As you can see, this ∗asterisk∗ has no impact on formatting here in the forum. But a standard SHIFT 8 (on my keyboard, at least) asterisk will turn the next word into italics and two standard asterisks will turn the final word in this sentence bold.

1 Like

An alternative way to make the code block stand out, could be to use backdrop-filter, which would adapt the background color. Like in the following variant:

.callout pre {
  background-color: inherit;
  backdrop-filter: brightness(120%);
} 

Which in light mode does this to an example of mine:

Adjust the percentage according to your liking, but when above a 100 %, it kind of pops the color of the callouts a little, and I thought it looked a little nice.

2 Likes

Just remembered that you can also set this up in Obsidian only.

  1. Create a template that contains just the asterisk you want to use
  2. Whenever you need that asterisk, call up the insert template modal (I use CMD /) and insert your template–asterisk. If the template is called aaaa it should be the first template in the modal. For me, CMD / and then Enter will insert the asterisk that doesn’t format anything in Obsidian
  3. If you use Templater, you can even assign a hotkey to insert the aaaa template immediately. I just set this up on my Mac as CTRL OPT CMD A. When I press that hotkey combo, I get my ∗.
  4. ∗∗Yippee∗∗
1 Like

This looks so nice! Thank you!

Offhand, do you know if there’s a way to adjust the spacing of a callout in live preview? In reading mode it looks perfect, like in your screenshots, but I’m almost always in live preview and the callout spacing is a little more compact in that view. Is there any CSS magic that can make the spacing more consistent with reading view?

Thank you for making me laugh at my desk! :smile:

Unfortunately I have a PC at work, which is where I run into this problem. But this is a great solution and I will keep it in mind for my other vaults that I use on Mac!

1 Like

I’m a little out on thin ice, but I believe that the spacing in live preview is something one shouldn’t mess to much with due to it being dependent on the line height, or something like that.

You can do this with templates in Obsidian on a PC:

Hmm, I tried this out and realized that it also has the same issue as the escaping character - if I copy/paste a search strategy to share with a student, I would have to replace the alternate asterisks with shift+8 asterisks for it to work when they paste it into the search. I think I will have to use the code block method for my very specific use case, which also has the advantage of giving an easy button to copy the entire block for sharing.

1 Like

That makes sense. I can live with a little bit of wonky spacing :slight_smile: Thank you again for your help!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.