I often want to write filenames for code.
Normally I write like this:
string = "This is example code"
Is this okay? Is there a standard Stack Overflow way of Markdown formatting for writing a filename?
Meta Stack Exchange is a question and answer site for meta-discussion of the Stack Exchange family of Q&A websites. It only takes a minute to sign up.Sign up to join this community
Since July 2021 (apparently) the previous method no longer works. Here's a new method based on tag stripping:
<h5 a><strong><code>hello_world.js</code></strong></h5> ``` js alert("Hello world!"); ```
<h5 a> tag gets stripped leaving its content "naked". Note that an attribute in
<h5 a> tag and a blank line after it are required!
You can make a very nice-looking named code block using small headers and inline code, like so:
#### **`hello_world.js`** ``` js alert("Hello world!"); ```
I've accidentally found it in this post on Formatting Sandbox and just don't want this great trick to get lost there as I was unsuccessfully looking for this some time before.
Though not as good as above, you could also specify a file name via language-specific comment before your code:
# hello_world.sh echo "Hello world!"
I'd never thought about it until seeing nicael's answer, but I kind of like "quoting the file".
@echo off setlocal enableDelayedExpansion set argument1=!%1! echo %argument1%>test.txt
@echo off echo %~1
Bolding is fine, though I think that the header markup you've used may be a little too forceful. Large text could help, though, if you had a lot of (hopefully small!) snippets in your post. I'm not sure we need a standard for this: use what makes your post most easily read and understood.
I will say, however, that code markup is definitely not the proper choice. The name of a file is not an element of a program; marking it as such is semantically incorrect.